x


Accessing children of instances vs children of original prefab

It looks like there is a major difference in accessing a child object using:

mainObject.transform.Find("Child");

vs

mainObject.Find("Child");

It looks like the first will access the children of the instance, and the second will access the children of the prefab (but not the instances)? Is that the main difference?

more ▼

asked Jan 04 '12 at 07:38 AM

ina gravatar image

ina
3.3k 492 549 598

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Are you using UnityScript and is mainObject a GameObject?

If that is the case, the first is calling the method Find from transform, while the second is calling the static method Find from GameObject class. The differences are:

1 - the 1st is a method from a Transform instance and the 2nd is static method from GameObject

2 - the 1st returns a Transform and the 2nd returns a GameObject

3 - the 1st searches in the children of the given transform, while the 2nd searches in all the scene

more ▼

answered Jan 04 '12 at 03:19 PM

luizgpa gravatar image

luizgpa
851 4 9

Can you explain the difference between a static method from GameObject and Transform instance? Does one access the original prefab, vs just an instance?

Jan 16 '12 at 07:36 AM ina

If mainObject is GameObject when you call mainObject.Find("Child") it actually calls GameObject.Find("Child"). In C# it would be an error because a GameObject doesn't have a Find method.

The main difference is #3. If you 4 objects with the same name GameObject.Find() will return the same one no matter from where it was called.

Jan 16 '12 at 03:10 PM luizgpa

Hmm I guess this means that there is no difference between the original prefab and its instances

Jan 19 '12 at 10:43 AM ina
(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:

x1279
x1255
x422
x184
x148

asked: Jan 04 '12 at 07:38 AM

Seen: 1395 times

Last Updated: Jan 19 '12 at 10:43 AM