Instantiate a prefab access child

Hello,
I have a prefab. Which has a car with four wheels a Camera. After instantiating I want to access Camera of that newly created object modify some parameters. How do I do it?
I was able to instantiate using the prefab. Having trouble accessing children.

Here`s how I have set the hierarchy,

Car
|->Body
| |->Wheel 1
| |->Wheel 2
| |->Wheel 3
| |->Wheel 4
|->Camera[/code]

Once you’ve instantiated your prefab object you can use transform.Find(…) to find the camera (or any other child object) by name.

Is the ‘Find()’ a hash lookup or does it iterate a list?

Is there ‘another’ way to do it, like:

transform[“scriptName”] as opposed to transform.Find(“scriptName”) ?

It iterates through the scene hierarchy (or just a child hierarchy if appropriate) looking for the first matching object using the name you provide, which means it can be slow and it’s best to reduce Find() usage and cache the results when possible (like in a variable/property that you keep).

And no, there is no lookup by name like that, you must use transform.Find() to find children of that transform with that name, or something like GameObject.Find() to find a game object anywhere in the hierarchy (not just a search through the children).

If I may, who/where are you needing to access the camera? Is it within a script found on one of the prefab’s game objects? If so then perhaps you should create a public variable in that script then drag-assign the camera game object or transform as that variable’s value. This incurs no “Find()” type efforts at run-time as you’re doing the work ahead of time.

You import a scene from a 3D app and can’t access an object in hierarchy directly? That’s incredible. I thought the whole point of using scripts was to avoid drag and drop?

You do have full scripting access. I’m not sure what your question on this old thread is?

I just want to refer to a known object in a hierarchy without searching ('find’ing) for it or creating my own dynamic array of everything

When I do this:

pBody = (transform.Find("body"));

I get this error:

error CS0030: Cannot convert type `UnityEngine.Transform' to `UnityEngine.GameObject'

But I need to find the body as a gamObject because I’m using “pBody” to control the animations of the character.

What am I doing wrong? I’ve spent a long time looking :frowning:

Thanks.

Hi CarlL

I am very new at Unity Scripting but I understand you need to cast it in order to work.

Try

    pBody = (transform.Find("body") as GameObject);

hope it works!

1 Like

Sorry for digging up old threads but seeing as how it didn’t get a proper answer (or at least one that would be satisfactory) and I ended up at the thread with the same question I thought I’d chip in.

The transform.Find/transform.FindChild retrieve a transform object and you need to access the gameObject attribute of the result value thus leaving you with the following line of code:

this.transform.FindChild("Body").gameObject

Of course this also applies to the Find method.

1 Like

THANKS exactly what i was searching for :slight_smile:

Thank you and keep digging old threads please :slight_smile:

Create a class with a camera property, then in the editor drag the camera of the child onto the script, then save the prefab.

using UnityEngine;

public class MainObject : MonoBehaviour {
    public Camera childCamera;
}

Then when you instantiate it, you will have access to it.

using UnityEngine;

public class CreationClass : MonoBehaviour {

    GameObject MyObject;

    void Start(){
        Camera cam = Instantiate(MyObject).GetComponent<MainObject>().childCamera;
       
        cam.farClipPlane = 1000;
        cam.nearClipPlane = 0;
    }

}

Holy necromancy…

anyway, lots of way to do this, I think
the best way is GetComponentInChild();