Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Instantiate a prefab access child

Discussion in 'Scripting' started by altaf.navalur, Feb 25, 2010.

  1. altaf.navalur

    altaf.navalur

    Joined:
    Jan 7, 2010
    Posts:
    12
    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]
     
  2. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Once you've instantiated your prefab object you can use transform.Find(...) to find the camera (or any other child object) by name.
     
  3. Bunzaga

    Bunzaga

    Joined:
    Jan 9, 2009
    Posts:
    202
    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") ?
     
  4. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    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.
     
  5. duke

    duke

    Joined:
    Jan 10, 2007
    Posts:
    763
     
  6. vished

    vished

    Joined:
    Oct 24, 2010
    Posts:
    26
    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?
     
  7. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    You do have full scripting access. I'm not sure what your question on this old thread is?
     
  8. vished

    vished

    Joined:
    Oct 24, 2010
    Posts:
    26
    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
     
  9. CarlL

    CarlL

    Joined:
    Nov 11, 2008
    Posts:
    83
    When I do this:

    Code (csharp):
    1. pBody = (transform.Find("body"));
    I get this error:

    Code (csharp):
    1. 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 :-(

    Thanks.
     
  10. largo17

    largo17

    Joined:
    May 2, 2012
    Posts:
    1
    Hi CarlL

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

    Try
    Code (csharp):
    1.  
    2.     pBody = (transform.Find("body") as GameObject);
    3.  
    hope it works!
     
    Flondy likes this.
  11. Wojtossfm

    Wojtossfm

    Joined:
    Oct 13, 2013
    Posts:
    1
    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:

    Code (csharp):
    1. this.transform.FindChild("Body").gameObject
    Of course this also applies to the Find method.
     
    Flondy likes this.
  12. Flondy

    Flondy

    Joined:
    Sep 27, 2014
    Posts:
    5
    THANKS exactly what i was searching for :)
     
  13. seif1992

    seif1992

    Joined:
    Apr 24, 2016
    Posts:
    1
    Thank you and keep digging old threads please :)
     
  14. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    Create a class with a camera property, then in the editor drag the camera of the child onto the script, then save the prefab.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class MainObject : MonoBehaviour {
    4.     public Camera childCamera;
    5. }
    6.  
    Then when you instantiate it, you will have access to it.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CreationClass : MonoBehaviour {
    4.  
    5.     GameObject MyObject;
    6.  
    7.     void Start(){
    8.         Camera cam = Instantiate(MyObject).GetComponent<MainObject>().childCamera;
    9.        
    10.         cam.farClipPlane = 1000;
    11.         cam.nearClipPlane = 0;
    12.     }
    13.  
    14. }
     
  15. astrokoala

    astrokoala

    Joined:
    Mar 23, 2016
    Posts:
    22
    Holy necromancy...

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