Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by velv · May 07, 2012 at 07:06 PM · javascriptparentobjectschildrenbuilding

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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
 }  
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image velv · May 07, 2012 at 10:33 PM 0
Share

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?

avatar image Piflik · May 08, 2012 at 09:59 AM 0
Share

Try

 var aColumns : GameObject[] = new GameObject[3];
avatar image velv · May 08, 2012 at 12:34 PM 0
Share

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; (?)

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to find an object in children 1 Answer

GetComponent searches an objects child instead of the object 2 Answers

Create multiple instances of an object 2 Answers

Update Parent/ Children From Script? 2 Answers

Get Children of Child and Deactivate/Activate on Demand 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges