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
1
Question by Luci85 · Apr 09, 2012 at 01:20 PM · prefabvariableinstance

GetComponent returns only the last instance

I have the following problem:
I have an array of unique instances of one Prefab, now I want to get 2 variables attached to each instance (it's anchor and a distance variable).
Here's how I create the array:

     var rubberBand:GameObject;
     private var uniqueRubberBand:GameObject;
     private var lastRubberPos:Vector3;
     private var rubberBandName:String;
     static var rubberBandNameArray = new Array ();
 
     uniqueRubberBand = Instantiate(rubberBand, linepos,  transform.rotation);                
     rubberBandName = "RubberBand";
     rubberBandName = rubberBandName.Insert(rubberBandName.Length, rubberBandNameArray.length.ToString());
     uniqueRubberBand.name = rubberBandName;
     rubberBandNameArray.Add(uniqueRubberBand.name);


And here's how I access it from another script:

 private var rubberBand:GameObject;
 private var rubberBandScript:Component;
 
 for(i = 0; i < DrawPathFullTrackScript.rubberBandNameArray.length; i++)
 
 rubberBand = GameObject.Find(DrawPathFullTrackScript.rubberBandNameArray[i]);
 rubberBandScript = rubberBand.GetComponent(RubberbandScript);
 anchor = rubberBandScript.anchor;
 distance = rubberBandScript.distance;


My problem is that I only get the anchor and the distance of the last created instance, even though every instance is uniquely named :-/

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

Answer by aldonaletto · Apr 09, 2012 at 01:58 PM

In the code you've posted, only the first instruction after the for is executed inside the loop - the others will be executed after the loop has ended, thus only the last object is modified.
You should enclose the loop instructions inside brackets:

for(i = 0; i < DrawPathFullTrackScript.rubberBandNameArray.length; i++){
  rubberBand = GameObject.Find(DrawPathFullTrackScript.rubberBandNameArray[i]);
  rubberBandScript = rubberBand.GetComponent(RubberbandScript);
  anchor = rubberBandScript.anchor;
  distance = rubberBandScript.distance;
}
Anyway, there's a much better way to do this: tag the clones as "RubberBand" (remember to register this tag!), get all of them in a GameObject array with GameObject.FindObjectsWithTag, then loop through the items with for:

var rubberBand:GameObject; private var uniqueRubberBand:GameObject; private var lastRubberPos:Vector3; static var rubberBandNameArray = new Array ();

 uniqueRubberBand = Instantiate(rubberBand, linepos,  transform.rotation);
 uniqueRubberBand.tag = "RubberBand"; // set the clone tag as RubberBand
 // it's easier to create the unique name this way:
 uniqueRubberBand.name = "RubberBand" + rubberBandNameArray.length.ToString();
 rubberBandNameArray.Add(uniqueRubberBand.name);

// When you need to set all RubberBand objects:

private var rubberBand:GameObject; private var rubberBandScript:RubberbandScript; // declare the correct type!

// get all RubberBand objects in an array: var rubberBands: GameObject[] = GameObject.FindGameObjectsWithTag("RubberBand"); // loop through the array for(rubberBand in rubberBands){ rubberBandScript = rubberBand.GetComponent(RubberbandScript); anchor = rubberBandScript.anchor; distance = rubberBandScript.distance; ... }

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 Luci85 · Apr 09, 2012 at 02:56 PM 0
Share

I only forgot to copy the brackets :-) Thanks for the optimising, but the mistake was elsewhere. I declared anchor and distance as static vars in the prefab, which apparently gets overwritten with each instance.

Edit: Got it to work, the prefab variables must be neither private nor static.

avatar image aldonaletto · Apr 09, 2012 at 03:19 PM 0
Share

I suspected that it could be a typo, but the symptoms matched the diagnostic so well... Glad to know that it's working now!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Updating variable on another script. It is not working. 2 Answers

Is it possible to set up a prefab variable such that it can only be modified on the prefab and not on the instance? 3 Answers

Updating a variable on a script in an instanced object 1 Answer

How can I modify a variable on an instance of a prefab after I created it? 2 Answers

Are there restrictions on assigning GameObjects to Prefab script variables? 3 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