- Home /
Referencing instantiated objects at runtime
How do you reference objects that are created at runtime?
I think what you mean is this?... you instantiate a bunch of objects at runtime, and then later want to be able to reference one or all of them, either to do something to them, or check what they're upto. Is that right?
yes basically - but they won't all be instantiated at once. depends on player input
Answer by Eric5h5 · Dec 23, 2010 at 03:32 AM
var clone = Instantiate (prefab);
// do stuff with the "clone" variable
wouldn't you have to know the size of the array before declaring it? N times is unknown, and it depends on how many times the player clicks during runtime - and it's not all at once
No, just use a list. var clones = new List.<GameObject>(); clones.Add (Instantiate (prefab));
okay - if these prefab's are moved after being declared, assigned to the clones variable, for example, would their transforms still be updated? - also, is it better to use tags or to just keep the variables in memory?
Answer by Justin Warner · Dec 23, 2010 at 03:30 AM
Could give it a tag, if their are multiple ones (Bullets or enemies?), you could make an array of FindObjectsWithTag or whatever...
If just one though, you can always just use tags.
$$anonymous$$ake it a prefab with the tag, and instantiate the prefab.