I am new to NGUI
and unity 3d
.
I have two panels in a ui
root. its named as firstPanel
and secondPanel
. secondPanel is deactivated in scene. In firstPanel
i have so many buttons and one is a play
button, that is image button. While Clicking on play
button, firstPanel
should get hide and secondPanel
should show.I adde a new Script to play
button and written code in it
void OnClick(){
GameObject panel2 = GameObject.Find("secondPanel");
NGUITools.SetActive(panel2,true);
GameObject panel1 = GameObject.Find("firstPanel");
NGUITools.SetActive(panel1,false);
}
But I get this Error : "NullReferenceException"
In which script of ngui
i have to edit and how can i do it? please help me to solve this issue
Thanks in advance.
If your panels are named as Panel1 and Panel2, you will not find them by using GameObject.Find("secondPanel") and GameObject.Find("firstPanel"). If "Panel1" and "Panel2" is the only name in the game scene(No other Panel1 or Panel2), then you can try to use
void OnClick(){
GameObject panel2 = GameObject.Find("Panel2");
NGUITools.SetActive(panel2,true);
GameObject panel1 = GameObject.Find("Panel1");
NGUITools.SetActive(panel1,false);
}
GameObject.Find("Something") cannot find any disabled game object, so you cannot use by this way. You can try to add reflection to your button code:
public GameObject pannel1 = null;
public GameObject pannel2 = null;
and set them to right panel in the scene editor window.
Another way, first you need to keep both of your panels active in your scene, then add code to your button script like this:
private GameObject panel1 = null;
private GameObject panel2 = null;
void Start()
{
panel1 = GameObject.Find("Panel1");
panel2 = GameObject.Find("Panel2");
}
void OnClick()
{
panel2.SetActiveRecursively(true);
panel1.SetActiveRecursively(false);
}
GameObject.Find(string name) function is not very efficient in Unity3D, so do not try to use it in Update() or every time you click your button.
I know this thread is old but if anyone else is having an issue calling panels when they are inactive you can prefab your panel and use the NGUITools.AddChild to call your panel. It would look something like this.
public GameObject parent;
public GameObject child;
void Spawn () {
NGUITools.AddChild (parent, child);
}
Assign your UIRoot to parent (or whatever you want to add the panel to), and assign your panel to child in the editor and you're all set! I hope this helps someone out, this is a common problem when first working with NGUI.
Happy Coding :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With