- Home /
Access Child of instantiated UI-Prefab using C# in Unity Beta 4.6
Hello, unityAnswers Community.
I am new to Unity, espacially to the new UI system in the Beta for 4.6.
What I want to achieve is the creation of an InfoPanel for several gameObjects. The InfoPanel will be a dragable PopupWindow that appears when clicking on the GameObject. After hours of research I managed to instantiate those InfoPanels from Prefabs. (I found this in the Unity documentation)
The Prefab I created looks like this:
My Code to Instantiate the object:
// On Mouse click the kidInfoPanel will appear
void OnMouseDown() {
// If the infoPanel is not active create the infoPanel
if (!windowActive) {
// Instantiate the Infopanel
panelInstance = Instantiate (panelPrefab) as GameObject;
panelInstance.name = gameObject.name;
// Set the infoPanel to the right canvas.
panelInstance.transform.SetParent (GameObject.Find ("Canvas_UI").transform, false);
windowActive = true;
}
}
All of this works fine, but I think that it will help solve my problem.
My Question: How can I now access "Text_Name" for example. So that when I click on the gameObject "Bob" it changes the text inside "Text_Name" to "Bob".
After several hours of forum search I came up with:
// Update is called once per frame
void Update () {
updateName ();
}
void updateName() {
GameObject text_Name = panelInstance.transform.Find("Text_Name").gameObject;
Text instruction = text_Name.GetComponent<Text> () as Text;
instruction.text = gameObject.name;
}
If I now run the program it shows:
NullReferenceException: Object reference not set to an instance of an object createKidInfoPanel.updateName () (at Assets/Scripts/createKidInfoPanel.cs:40) createKidInfoPanel.Update () (at Assets/Scripts/createKidInfoPanel.cs:36)
The line this refers to is:
GameObject text_Name = panelInstance.transform.Find("Text_Name").gameObject;
The code I posted is from a script called "createKidInfoPanel" that is attached to several "kid"-gameObjects. All of them should have their own InfoPanel containing information about this specific gameObject.
I tried for hours now and I am a bit clueless of what to do. This is also my first post in a Forum, so I tried to be as specific as possible.
Thanks, Azhra
Answer by Azhra · Oct 28, 2014 at 09:43 AM
After looking into my problem for quite some time now, I noticed my mistake. It was a rather stupid one.
// Update is called once per frame
void Update () {
updateName ();
}
I update the data on my prefab even if it is not instantiated. I replaced it with:
// Update is called once per frame
void Update () {
if (windowActive) {
updateName ();
}
}
Now everything works perfectly fine.
I hope that this post can atleast help other people stuck on a similar problem.
Your answer
Follow this Question
Related Questions
my object keeps cloning is self i want it to clone once 2 Answers
Instantiate PreFab with C# Script 5 Answers
Instantiate A Prefab 1 Answer
Instantiate in front of player (and give velocity) 1 Answer
Change prefab before instantiation 1 Answer