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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Xab6891 · May 03, 2014 at 09:14 PM · instantiateprefabresources.load

How to get unique reference to instantiated prefab

Hi, I'm looking for a way to create a player GUI using sprites and textmeshes. The game will have up to four players going in turns, and the location of each player's stats will be dependent on the number of players playing, and the randomized order they go in. Therefore, I'm looking to be able to automatically generate these game objects as the game starts up. I want to use clones of prefabs for the textmeshes and sprites to keep the code simple. What I'm discovering is when I try to update either class(in this case Inst), only the last text gets updated.

Here's an example of the class:

 public class Inst : MonoBehaviour {
     
     private GameObject textGO;    // prefab with a TextMesh
     private GameObject textCloneGO;
     
     public void init(int posX){
         textGO = Resources.Load("Prefabs/MaxTimeText") as GameObject;
         textCloneGO = Instantiate(textGO) as GameObject;
         textCloneGO.transform.position = new Vector3(posX,0,0);
         textCloneGO.name = "Inst" + posX.ToString();
     }
     public void setText(string text){
         textCloneGO.GetComponent<TextMesh>().text = text;
     }
 }

This is the class I used to test:

 public class InstTest : MonoBehaviour {
 
     private Inst i;
     private Inst i2;
 
     void Start () {
         i = gameObject.AddComponent<Inst>().GetComponent<Inst>();
         i2 = gameObject.AddComponent<Inst>().GetComponent<Inst>();
         i.init(-1);
         i2.init(1);
         i.setText("i1"); // changes i2 to "i1"
         i2.setText("i2"); // changes i2 to "i2"
     }
 }

An explanation as to what's going on, and suggestions on how to get the effect I'm going for would be appreciated.

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
1
Best Answer

Answer by DMGregory · May 03, 2014 at 09:36 PM

I think this is your error:

  i = gameObject.AddComponent<Inst>().GetComponent<Inst>();
  i2 = gameObject.AddComponent<Inst>().GetComponent<Inst>();

Try this instead:

  i = gameObject.AddComponent<Inst>();
  i2 = gameObject.AddComponent<Inst>();

AddComponent() on its own returns a reference to the newly-instantiated component. By calling GetComponent() from that reference, you get a reference to some component of that type, not necessarily the one you created on that line. In this case, i2 ends up getting a reference to the component created on the line above.

Comment
Add comment · Show 2 · 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
avatar image robertbu · May 03, 2014 at 09:37 PM 0
Share

Nicely spotted.

avatar image Xab6891 · May 03, 2014 at 10:07 PM 0
Share

I removed the GetComponent and the code works as expected. Thanks!

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Resources.Load() or Prefab manager? 1 Answer

Instantiate a prefab vs create one dynamically at runtime? 1 Answer

Spawning a prefab at another object's location 3 Answers

Instantiate individual components of a prefab? 1 Answer

Issue Instantiating prefab in C# 0 Answers


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