- Home /
Object Reference Not Set As Instance error from ngui code
hey all, not sure if many of you use ngui but il ask anyway. im using unity with the free version of ngui and im still quite new to it. my code gives this console error:
"NullReferenceException: Object reference not set to an instance of an object NGUITools.SetActive (UnityEngine.GameObject go, Boolean state) (at Assets/NGUI/Scripts/Internal/NGUITools.cs:613) CharacterControler.UseInventory () (at Assets/Script/CharacterControler.cs:288) CharacterControler.Update () (at Assets/Script/CharacterControler.cs:51)"
is this a ngui issue or has my code gone wrong? my code is here if you need it:
void Update () {
if(Input.GetButtonDown("Inventory") && canInventory){
canInventory = false;
StartCoroutine(InvTimer(1));
UseInventory();
}
}
void UseInventory(){
GameObject HudPanel = GameObject.Find("HudPanel");
GameObject InventoryPanel = GameObject.Find("InventoryPanel");
if(!inInventory){
Debug.Log("inInventory");
Screen.lockCursor = false;
NGUITools.SetActive(InventoryPanel,true);
//NGUITools.SetActive(HudPanel,false);
inInventory = true;
}
else if (inInventory){
Debug.Log("not inInventory");
Screen.lockCursor = true;
NGUITools.SetActive(InventoryPanel,false);
//NGUITools.SetActive(HudPanel,true);
inInventory = false;
}
}
public IEnumerator InvTimer(float delay){
yield return new WaitForSeconds(delay);
canInventory = true;
}
}
Best guess is that InventoryPanel
is not found, so you may be passing a null reference into NGUITools.SetActive()
. That being said, I had a quick look at the function source in NGUI, and it does appear to do a null check itself, so I'm a little confused :( Are you using the most up to date version of NGUI?
nah i dont have $60 spare so im just using the free version
All I can really suggest is that you do a null check on InventoryPanel
, and make sure it's named correctly in the scene.
it is, otherwise i wouldnt be able to do a NGUITools.SetActive(InventoryPanel, false); the problem (i think) is that if its disabled then i cant access it and enable it because it the properties cease to exist when its disabled if that makes any sense at all
Answer by alwayscodeangry · Jul 20, 2014 at 07:02 AM
Yes, you're actually pretty close. I should have spotted this first time round, but GameObject.Find()
won't work on an inactive GameObject.
Try finding the object on Awake and holding a reference, or set a public reference via the inspector. It'll be more efficient than calling GameObject.Find()
at game time as well :)
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
C# script error. 0 Answers
Show Strings With Last Characters of the first Word 1 Answer
error CS8025: Parsing error in C# code 3 Answers
Null in GetValidMethodInfo 0 Answers