NullReferenceException: Object reference not set to an instance of an object ColpiLef

Hi folks, i’ve a problem with a script. He show me an error:
NullReferenceException: Object reference not set to an instance of an object
ColpiLeft.UpdateGUI () (at Assets/Scripts/HUD/ColpiLeft.js:12)

I’m trying to call a function from another script without declaring static variables. The GetBulletsLeft() function return the bullets left. Can someone help me, please? :smile:
This is the js code!

private var weapon : WeaponControl;



function Awake(){

	weapon = new GetComponentInChildren(WeaponControl);

}



function LateUpdate(){

	UpdateGUI();

}



function UpdateGUI(){
		guiText.text = "Prova"+weapon.GetBulletsLeft().ToString();
}

weapon is unassigned.
Either A) You’ve not created a WeaponControl on a child GameObject, or B) The Update ordering is messed up and Awake() is being called before the WeaponControl is created.

If another script is creating the WeaponControl, they you can use Start() for a little safety. Otherwise you can check if Weapon is null in UpdateGUI() and not report it if it’s not there. Or you could make weapon public and manually assign it.