Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Azhra · Oct 25, 2014 at 01:58 PM · uiinstantiateprefabaccess

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:

alt text

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

prefab_ui.png (8.0 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges