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.
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?
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: