x


Referencing instantiated objects at runtime

How do you reference objects that are created at runtime?

more ▼

asked Dec 23 '10 at 02:53 AM

ina gravatar image

ina
3.3k 492 549 600

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?

Dec 23 '10 at 04:26 AM dissidently

yes basically - but they won't all be instantiated at once. depends on player input

Dec 23 '10 at 04:28 AM ina
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first
var clone = Instantiate (prefab);
// do stuff with the "clone" variable
more ▼

answered Dec 23 '10 at 03:32 AM

Eric5h5 gravatar image

Eric5h5
80.3k 42 132 521

what if you have N clone's, where N is arbitrary?

Dec 23 '10 at 04:09 AM ina

You can use an array.

Dec 23 '10 at 04:24 AM Eric5h5

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

Dec 23 '10 at 04:29 AM ina

No, just use a list. var clones = new List.<GameObject>(); clones.Add (Instantiate (prefab));

Dec 23 '10 at 04:39 AM Eric5h5

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?

Dec 23 '10 at 06:50 AM ina
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Dec 23 '10 at 03:30 AM

Justin Warner gravatar image

Justin Warner
6.3k 19 27 65

how do you assign tag's at runtime?

Dec 23 '10 at 04:10 AM ina

Make it a prefab with the tag, and instantiate the prefab.

Dec 23 '10 at 08:41 PM Justin Warner
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2087
x1674
x1095
x374
x263

asked: Dec 23 '10 at 02:53 AM

Seen: 1879 times

Last Updated: Dec 23 '10 at 02:53 AM