how to instantiate an object inside a hierarchy?

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’.

static var imr : float = 41.5; // position x

// this fuction is called from somewhere else
static function DrawMRcurve () {
	
	var dot = GameObject.Find("CurveDot"); // this is the dot object

	Instantiate (dot, Vector3(imr,OtherScript.mr,0), Quaternion.Euler(Vector3(270, 0, 0)));

	imr += -5.00; // increment x

}

transform.parent is your friend :slight_smile:

Hello, doit like this:

static var imr : float = 41.5; // position x 

// this fuction is called from somewhere else 
static function DrawMRcurve () { 
    
   var dot = GameObject.Find("CurveDot"); // this is the dot object 

   var tmpObj = Instantiate (dot, Vector3(imr,OtherScript.mr,0), Quaternion.Euler(Vector3(270, 0, 0))); 
   tmpObj.parent = dot;

   imr += -5.00; // increment x 

}

Hope it helps :wink:

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.

static var posXmc : float = 43.5; // position x

// is called from GUI
static function DrawMCcurve () {
	
	var dot = GameObject.Find("MCdot"); // the Dot-Object to clone	
	var root = GameObject.Find("CurveRoot").transform; // Get the transform of the root object
	
	var tmpObj = Instantiate (dot,  .... );
	
	tmpObj.transform.parent = root; // make child of root
	 

	posXmc += -5.00; // increment x

}

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.

I am also getting same error and I am also new to unity, so don’t know much about debugging :sweat_smile:

Try:

var tmpObj : GameObject = Instantiate(...);

This ensures that tmpObj is of the correct type (namely GameObject) and, as a result, allows you to get at the transform.

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.