How do I create a parent,with some children in javascript?

What I can’t figure out is how to: Make a parent, with some children in javascript.

I am creating a simple game where the player creates a shape out of some boxes. The next level instantiates the boxes is the shape the player created. Then the player will then move the object they created, all the boxes are connected together and move as unit. One of the boxes is the main controller and the other boxes are armor. The armor will be destroyed when it is hit.

I have found the combine children scritpt on the forms, however I do not think that would work in my case, since I don’t want to combine the meshes. I have tried, it is little to complex for me to figure out. :frowning:

I am creating the boxes by:

var cbox : Transform; // Controller box. I want this to be the parent.
var abox1 : Transform; //1 armor box. This will be a child.

function Start () {
Instantiate(cbox, Vector3 (500, 11, 500), Quaternion.identity);
Instantiate(abox1, Vector3 (500, 10, 500), Quaternion.identity);
}

I am really new to this programing and think that I am overlooking something super simple. :?

I have tried just about everything I could think to try with the Transform.parent and just can’t get it to work. I have used the unity manual and search the forums.

What I can’t figure out is how to: Make the cbox the parent, with the instantiate boxes as the child

In unity it is a simple drag an drop to make it a child. :smile: I just can’t find out how to do it in javascript. :?

Maybe I am going about this in the wrong way, is there any other way to attach the boxes together.

Thanks,
Confused

You just need some way to refer to the instantiated objects so you can do the parent thing, hence you can assign variables to them, like so:

var cbox : Transform;      //  Controller box.  I want this to be the parent.
var abox1 : Transform;   //1 armor box. This will be a child.

function Start () {
	var newCbox : Transform = Instantiate(cbox, Vector3 (500, 11, 500), Quaternion.identity);
	var newAbox1 : Transform = Instantiate(abox1, Vector3 (500, 10, 500), Quaternion.identity);
	newAbox1.parent = newCbox;
}

–Eric

Thank you !!! It works thanks :smile:

Is there any way to get an array to work with the transform.parent. I want to use it in a for loop. I have defined it as a float and have specified the float size.

var armor : float[];

function Start () {
armor = new float[5];

for (a=0;a>4;a++)
{
var armor[a] : Transform = Instantiate(Armor, Vector3 (armor[x], 10, armor[y]), Quaternion.identity);  // This is the line that is having problems 
armor[a].parent = armorParent;
}
}

error UCE0001: “;” expected. Insert a semicolon at the end.
error BCE0043: Unexpected token: :.

You’ve got armor defined as a float, but then later you’re also trying to define it as a Transform. A float is just a floating point number, which isn’t what you want here. You can try this:

var armor : Transform[]; 

function Start () { 
    armor = new Transform[5]; 

    for (i = 0; i <= 4; i++) 
    { 
        armor[i] = Instantiate(armorPrefab, Vector3(0, 10, 0), Quaternion.identity);
        armor[i].parent = armorParent; 
    } 
}

I changed some things…“i” is the traditional iterator variable name; “a” is used in Unity to refer to the alpha component of a color (like “myColor.a = .5”), so using “a” as an iterator is a little confusing. I assume you wanted all 5 armors to be used, so I used “<= 4”. Using “Armor” as a variable name is confusing when you have “armor” for the array, so I used armorPrefab (which assumes you actually have a public variable called armorPrefab that you’re linking up in the Inspector). It’s easier to keep things straight if you stick to lowercase for variable names and uppercase for classes/functions. I wasn’t quite sure what you wanted with the Vector3, so it probably doesn’t work the way you want (all of them will be instantiated at the same spot right now), but you can of course change that.

–Eric

I had a little trouble at first didn’t know what kind of var to make the Parent and prefab :slight_smile: Then I had to assign a empty game object to my parent in inspector, and also add my prefab.

Thanks for your help!! I couldn’t figure it out on my own. Also appreciate the coding tips, I am very new to this. :smile:

var armorPrefab : Transform;
var armorParent : Transform;  

var armor : Transform[]; 

function Start () { 
    armor = new Transform[5]; 

    for (i = 0; i <= 4; i++) 
    { 
        armor[i] = Instantiate(armorPrefab, Vector3(0, 10, 0), Quaternion.identity); 
        armor[i].parent = armorParent; 
    } 
}

[/code]