- Home /
Updating a variable on a script in an instanced object
I'm still trying to grok Unity's naming system - I apparently still can't figure out how to update the variable on a script inside an instanced object (prefab)!
Prefab Ford
has script CarsInternal.js
, which has an exposed variable var cars:int;
- There can be N instances of Ford, as Ford is a prefab.
A single script, External.js
in a single object Brands
is trying to update the cars
integer in Ford. How do you update that variable from External.js
?
why did someone give this question a downgrade mark? It's a VERY good question. Very useful to those of us not gifted with innate understanding and comprehension of the ways of Unity. The official documentation is a LONG way from clear about this and does not explicitly deal with this situation.
it looks like someone has been stalking down my questions and downvoting them..
Dunno' about your other questions, but I down-voted this question because it has been asked and correctly answered many many times before.
Clicking the Variable tag would of yielded at least 10 similar questions in the first page alone (one of them is even identical in wording to yours). I assume any Answers member will first search before posting yet another question that just obscures the real questions.
well i usually search both stack and google before posting, and i couldn't find one that answered my question, so if you've found duplicates, you are free to post the links here
If you would of read my comment and pressed the Variable tag button you would of immediately found a couple of correct answers. For accessing another script:
http://answers.unity3d.com/questions/26832/cant-access-prefabs-variables-once-instantiated http://answers.unity3d.com/questions/16543/get-a-variable-from-another-script-in-unity-iphone Get an array of references ( by tag): http://answers.unity3d.com/questions/2336/how-do-i-count-or-get-references-to-all-gameobjects-that-are-tagged-with-a-partic and so on.
Answer by azzogat · Dec 27, 2010 at 08:38 AM
See the second example from the scripting reference (linked below). Basically, create an array of all instantiated prefabs (by tag) and loop through them. Instead of what they do in the reference page, just reference the said script like:
var CarsInternalScript: CarsInternal = go.GetComponent('CarsInternal') as CarsInternal;
and access it like:
CarsInternalScript.cars = 1337;
FindGameObjectsWithTag:
http://unity3d.com/support/documentation/ScriptReference/GameObject.FindGameObjectsWithTag.html
GetComponent:
http://unity3d.com/support/documentation/ScriptReference/Component.GetComponent.html
If you mean changing the variable of a prefab before you instantiate it, check this manual page (and especially the Instantiating rockets & explosions and Replacing a character with a ragdoll or wreck part):
http://unity3d.com/support/documentation/Manual/Instantiating%20Prefabs.html
Hope this helped.
Check the links I provided (as I also state in my answer). Replace what they do in the loop with what I've written here.
this works - but it does not seem to catch the first one, i.e., the gameobject-to-be-instantiated that is already in the scene
and i guess that is my question, how do you catch all of them, and not just most of them.
If you mean instantiating a prefab and changing one of it's variables before instantiating, take a look at the various snippets here: http://unity3d.com/support/documentation/$$anonymous$$anual/Instantiating%20Prefabs.html especially the Instantiating rockets & explosions part.
Your answer
Follow this Question
Related Questions
How can I modify a variable on an instance of a prefab after I created it? 2 Answers
Instantianig a teleporter prefabs with destination variables 1 Answer
[Solved]Instantiating prefab from a script and destroy it from another one 2 Answers
Issues with tracking prefab instances and gameobject naming (JS) 1 Answer
What is the difference between grey and blue prefab ? 1 Answer