x


how to put the objects together (javascript)

Description: I want to make a building by script. THe building has its parts as a flat, a parvis, a ground floor, a second floor etc. In the script below i made functions for each part of the building. Then in the main function start() i create these parts and next i try to combine them under their 'father object' flat.

Here's the script:

class IonianHouse{
//create the shapes of IonianHouse building parts
var oParvis = GameObject.CreatePrimitive(PrimitiveType.Cube);
var oFlat = GameObject.CreatePrimitive(PrimitiveType.Cube);
var oGroundFloor =  GameObject.CreatePrimitive(PrimitiveType.Cube);
var oFirstFloor =   GameObject.CreatePrimitive(PrimitiveType.Cube);
var oLeftCube =     GameObject.CreatePrimitive(PrimitiveType.Cube);
var oRightCube = GameObject.CreatePrimitive(PrimitiveType.Cube);
var oFirstFloorRoof = GameObject.CreatePrimitive(PrimitiveType.Cube);
var oColumn = GameObject.CreatePrimitive(PrimitiveType.Cylinder);

var rot: Quaternion = Quaternion.identity;

function Collumns(){

    var cloneColumn = Instantiate(oColumn , Vector3(-79.01502,3.930318,10.8331), rot);

    //Choose a Structure, which is more capable for the collumn objects    
    var aColumns : GameObject[3];

    oColumn.transform.localScale.x =0.6616377; //X scale of the collumn
    oColumn.transform.localScale.y =3.189006; //Y scale of the collumn
    oColumn.transform.localScale.z = 0.9843928; //Z scale of the collumn

    //Set the Collumns
    for(var i =0; i<=aColumns.length; i++){
       //puts in the Array the oColumn object clones        
       aColumns[i] = cloneColumn;
       cloneColumn.Vector3.x += -2; // puts every oColumn object clone near to the next one, X point distance of every clone is -2 
       }
}

function GroundFloor(){
    var pos = oGroundFloor.transform.position;
    pos = Vector3(-82.50504,3.930318,30.67108);
    oGroundFloor.transform.localScale.x = 14.22;
    oGroundFloor.transform.localScale.y = 6.35;
    oGroundFloor.transform.localScale.z = 32.66;
    Instantiate(oGroundFloor, pos, rot);
}

function FirstFloor(){
    var pos = oFirstFloor.transform.position;
    pos = Vector3(-82.50504,9.904648,27.14175);
    oFirstFloor.transform.localScale.x = 14.22;
    oFirstFloor.transform.localScale.y = 5.53;
    oFirstFloor.transform.localScale.z = 18.01;
    Instantiate(oFirstFloor, pos, rot);
}

function Flat(){
    var pos = oFlat.transform.position;
    pos = Vector3(-82.49211,0.4076467,27.69778);
    oFlat.transform.localScale.x = 14.22;
    oFlat.transform.localScale.y = 0.78;
    oFlat.transform.localScale.z = 38.55168;
    Instantiate(oFlat, pos, rot);
}

function LeftCube(){
    var pos = oLeftCube.transform.position;
    pos = Vector3(-77.17271,3.258567,12.60766);
    oLeftCube.transform.localScale.x = 3.62;
    oLeftCube.transform.localScale.y = 4.97;
    oLeftCube.transform.localScale.z = 3.61;
    Instantiate(oLeftCube, pos, rot);
}

function RightCube(){
    var pos = oRightCube.transform.position;
    pos = Vector3(-87.83255,3.258567,12.60766);
    oRightCube.transform.localScale.x = 3.62;
    oRightCube.transform.localScale.y = 4.97;
    oRightCube.transform.localScale.z = 3.61;
    Instantiate(oRightCube, pos, rot);
}

function Parvis(){
    var pos = oParvis.transform.position;
    pos = Vector3(-87.12512,-0.006041527,51.18095);
    oParvis.transform.localScale.x = 20.05;
    oParvis.transform.localScale.y = 0;
    oParvis.transform.localScale.z = 15.84;
    oParvis.transform.rotation.y = 36.25169;
    Instantiate(oParvis, pos, rot);
}

function Start () {
    Parvis();
    Flat();
    GroundFloor();
    FirstFloor();
    RightCube();
    LeftCube();
    Collumns();

    if (oFlat != null){
       //conect the objects: parent-children
    oFlat.transform.parent = GameObject.Find("oGroundFloor").transform;
    oFlat.transform.parent = GameObject.Find("oParvis").transform;
    oFlat.transform.parent = GameObject.Find("oRightCube").transform;
    oFlat.transform.parent = GameObject.Find("oLeftCube").transform;
    oFlat.transform.parent = GameObject.Find("oFirstFloor").transform;
    oFlat.transform.parent = GameObject.Find("oColumn").transform;
    }

} } 

Is there something wrong with the code according to what i want to do? Thanks in advance.

more ▼

asked May 07 '12 at 07:06 PM

velv gravatar image

velv
21 3 12 16

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

In the last section of your code you set the flat to be the child of every object in turn. You have to reverse this. Call the Flat() function first. Then you can set the Flat to be the parent of each object directly in the respective function. Example:

private var flat : Transform = null;    //variable to store the flat object

function Flat(){
    var pos = oFlat.transform.position;
    pos = Vector3(-82.49211,0.4076467,27.69778);
    oFlat.transform.localScale.x = 14.22;
    oFlat.transform.localScale.y = 0.78;
    oFlat.transform.localScale.z = 38.55168;
    flat = Instantiate(oFlat, pos, rot);    //store the flat object
}


function LeftCube(){
    var pos = oLeftCube.transform.position;
    pos = Vector3(-77.17271,3.258567,12.60766);
    oLeftCube.transform.localScale.x = 3.62;
    oLeftCube.transform.localScale.y = 4.97;
    oLeftCube.transform.localScale.z = 3.61;
    var tempObj = Instantiate(oLeftCube, pos, rot);
    if(flat != null)
        tempObj.parent = flat;        //child the new object to the flat
}  
more ▼

answered May 07 '12 at 07:30 PM

Piflik gravatar image

Piflik
5.4k 15 26 44

I see your point, thanks. There's another problem btw. I have an exception:

"Assets/IonianHouse.js(23,42): UCE0001: ';' expected. Insert a semicolon at the end." (IonianHouse is the name of the file.js) And marks that line: "var aColumns : GameObject[3];" ,where i create my array. Do you know why?

May 07 '12 at 10:33 PM velv

Try

var aColumns : GameObject[] = new GameObject[3];
May 08 '12 at 09:59 AM Piflik

I made some changes to my Array type:

var aColumns = new Array(); //size=3

aColumns.Push(cloneColumn); //sets the 1st column to the initial position

for(var i =1; i<=3; i++) { aColumns.Push(cloneColumn); //sets the rest 3 columns to the new given positions

if(flat != null){ //child the new object to the flat

cloneColumn.parent = flat; }

       cloneColumn.Vector3.x += -2; 

}

Now i get the exception: "Unknown identifier: 'Instantiate'." -- As for the parenting, is it: tempObjLC.parent = flat; or: tempObjLC.transform.parent = flat; (?)

May 08 '12 at 12:34 PM velv
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3444
x418
x321
x183
x67

asked: May 07 '12 at 07:06 PM

Seen: 447 times

Last Updated: May 08 '12 at 12:37 PM