Search Unity

how to instantiate an object inside a hierarchy?

Discussion in 'Scripting' started by JohannChristoph, Sep 1, 2009.

  1. JohannChristoph

    JohannChristoph

    Joined:
    Jun 21, 2009
    Posts:
    141
    hello Forum
    i have to make a kind of curve graph. So instantiate a kind of dot-object with incrementing x position and assign the current value to y. That works well.
    But i have a root object with which i want to bring the graph in position in front of the camera. But my dots get cloned outside of the hierarchy (the original dot is a child of that root object).
    How can i instantiate my dots inside the hierarchy?

    this is my code. The Curve shows a value called 'mr'.

    Code (csharp):
    1. static var imr : float = 41.5; // position x
    2.  
    3. // this fuction is called from somewhere else
    4. static function DrawMRcurve () {
    5.    
    6.     var dot = GameObject.Find("CurveDot"); // this is the dot object
    7.  
    8.     Instantiate (dot, Vector3(imr,OtherScript.mr,0), Quaternion.Euler(Vector3(270, 0, 0)));
    9.  
    10.     imr += -5.00; // increment x
    11.  
    12. }
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    transform.parent is your friend :)
     
  3. jmunozar

    jmunozar

    Joined:
    Jun 23, 2008
    Posts:
    1,091
    Hello, doit like this:
    Code (csharp):
    1.  
    2. static var imr : float = 41.5; // position x
    3.  
    4. // this fuction is called from somewhere else
    5. static function DrawMRcurve () {
    6.    
    7.    var dot = GameObject.Find("CurveDot"); // this is the dot object
    8.  
    9.    var tmpObj = Instantiate (dot, Vector3(imr,OtherScript.mr,0), Quaternion.Euler(Vector3(270, 0, 0)));
    10.    tmpObj.parent = dot;
    11.  
    12.    imr += -5.00; // increment x
    13.  
    14. }
    15.  
    Hope it helps ;)
     
  4. JohannChristoph

    JohannChristoph

    Joined:
    Jun 21, 2009
    Posts:
    141
    thanx for the help so far. I tried transform.parent.

    this code is attached to the root. it even works if it is not attached to anything.

    Code (csharp):
    1. static var posXmc : float = 43.5; // position x
    2.  
    3. // is called from GUI
    4. static function DrawMCcurve () {
    5.    
    6.     var dot = GameObject.Find("MCdot"); // the Dot-Object to clone 
    7.     var root = GameObject.Find("CurveRoot").transform; // Get the transform of the root object
    8.    
    9.     var tmpObj = Instantiate (dot,  .... );
    10.    
    11.     tmpObj.transform.parent = root; // make child of root
    12.      
    13.  
    14.     posXmc += -5.00; // increment x
    15.  
    16. }
    but the console says:
    'transform' is not a member of 'UnityEngine.Object'.

    it can't access the transform of the tmpObj. Maybe it doesn't know to which actual instance it belongs to.

    But even if it would find it. Unity is so 'intelligent' to keep the position in global space.

    maybe i have to find a total other solution, not with instances. But not today. it's quite late here, lol.
     
  5. Meekal

    Meekal

    Joined:
    Sep 1, 2009
    Posts:
    1
    I am also getting same error and I am also new to unity, so don't know much about debugging :oops:
     
  6. Jim Offerman

    Jim Offerman

    Joined:
    Jul 17, 2009
    Posts:
    177
    Try:

    Code (csharp):
    1.  
    2. var tmpObj : GameObject = Instantiate(...);
    3.  
    This ensures that tmpObj is of the correct type (namely GameObject) and, as a result, allows you to get at the transform.
     
  7. JohannChristoph

    JohannChristoph

    Joined:
    Jun 21, 2009
    Posts:
    141
    oh yes, yes thank you!!!

    it works well now. To finally bring my dots in place i just needed to assign the transformations to the tmpObj afterwards, in local space.