panel hide and show in NGUI
Solution 1
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);
}
Solution 2
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.
Solution 3
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 :)
Sona Rijesh
Working as Web Developer. Working in PHP (codeigniter, Yii, Wordpress) Unity3d
Updated on July 03, 2022Comments
-
Sona Rijesh almost 2 years
I am new to
NGUI
andunity 3d
. I have two panels in aui
root. its named asfirstPanel
andsecondPanel
. secondPanel is deactivated in scene. InfirstPanel
i have so many buttons and one is aplay
button, that is image button. While Clicking onplay
button,firstPanel
should get hide andsecondPanel
should show.I adde a new Script toplay
button and written code in itvoid 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 ofngui
i have to edit and how can i do it? please help me to solve this issue Thanks in advance. -
Sona Rijesh about 11 yearssorry. its not working properly. secondpanel is inactive in scene. so i cant able show it.
-
onevcat about 11 yearsSo I think keep them as reference will work. Try this: Add "public GameObject panel2;" in your script(in the class body, for example just above the void OnClick). Then select your play button, you will see a field called "Panel2" on your script in the Inspector. Drag the secondPanel from game scene to the field. Then you can delete the GameObject line and use panel2 directly.
-
GeekPeek over 8 yearsWhy use GameObject.Find and NGUITools.SetActive? That's so slow... Isn't there a way to just not render?