我必须从这个类访问变量 ammoMagazine
public class Pistol : MonoBehaviour {
public int ammoMagazine = 7;
}
然后我尝试了这段代码:
public class AmmoCounter : MonoBehaviour {
public int ammo;
private Pistol _pistol;
void Start () {
_pistol = GetComponentInChildren<Pistol>();
}
void Update () {
ammo = _pistol.ammoMagazine;
guiText.text = "Pistol: " + ammo + "/7";
}
}
为什么会有 NullReferenceException: Object reference not set to an instance of an object ?谢谢,我是 C# 新手