Get reference to nested prefab on same GameObject

A novice question and yes I have read the GetComponent reference. However, still having a problem getting this to work.

In my hierarchy I have a prefab with a Class Script component. Lets call this “ParentClass”.

Nested 2 levels deep in the hierarchy on the SAME prefab I have another Light prefab with name “foo”(in the hierarchy).

In the Start method for the ParentClass I have>

Light sideLight2 = gameObject.GetComponent(“foo”) as Light;

This doesn’t work.

Can anyone tell me what I am doing wrong?
Do I have to resort to GameObject.Find instead? I’ve read the latter is quite inefficient and should be avoided.

I think your use of the word “prefab” is confused, not to mention confusing. GetComponent is used to access a component (script) of a specific Type (script name/class name) attached to the GameObject. Your “foo” and “Light” should be the same word, because it’s the Type (class name), just written out as a string instead. Try using

gameObject.GetComponent<Light>()

instead, if Light is your script/class name, or Foo if that’s your script/class name. Next time, try just copying whatever code you have and/or taking screenshots that show the problem you’re having.

EDIT: I now think you’re talking about child GameObjects of the current object, in which case you either need a direct reference of the child object, to use GetComponentInChildren(), or GetComponentsInChildren(). If you use GetComponentsInChildren, you can iterate over the results and check the result.name == “foo” to find the one that you want.

Light[] lights = GetComponentsInChildren<Light>();
foreach(Light light in lights)
{
   if(light.name == "foo")
      sideLight2 = light;
}

You could also just iterate over all children of the current object first to find the right child, then grab the component from that child, like so:

foreach(Transform child in transform)
{
   if(child.name == "foo")
      sideLight2 = child.gameObject.GetComponent<Light>();
}

If your script is in the parent class and you are trying to access a script in the lower level hiearchy (foo in this case), then use the GetComponentInChildren function and pass foo in as the argument.

Thanks! Looks like GetComponentsInChildren is what I need. Thought I could get a direct reference without iteration but that doesn’t seem possible.

You can get a reference to the GameObject with Transform.Find. It will look for a GameObject of the specified name in the children of the object its called on.

2 Likes

Had no idea that GameObject.Find and Transform.Find worked differently, so very good to know. I’ve never actually used either though, lol.

GameObject.Find gets a bad rep because it searches through every GameObject in the scene. This can be very slow if its called multiple times per frame in a large scene.

By contrast Transform.Find searches only the children of the transform. Its generally not that slow, as there are not a lot of children to search (You should still cache the result). It makes for a great solution where you need to access specific children of a GameObject by their hierarchy location.

1 Like