- Home /
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.
Answer by Piflik · May 07, 2012 at 07:30 PM
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
}
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?
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: "$$anonymous$$ identifier: 'Instantiate'."
As for the parenting, is it: tempObjLC.parent = flat; or: tempObjLC.transform.parent = flat; (?)