• 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 23, 2012 at 07:00 PM · arraywindowclone

Placing parallel window objects into a building

hi, i have a building (which is generated randomly in the area) in which i'm trying to place on its facade a few windows (for example: 4 windows in an horizontal row).

Here's my code: function Start () {

  var oHouse = GameObject.CreatePrimitive(PrimitiveType.Cube);
  var oWindow = GameObject.CreatePrimitive(PrimitiveType.Cube);
  var minX: float;
  var maxX: float;
  var minZ: float; 
  var maxZ: float;
 
 minX = -100;
 maxX = 100;
 minZ = -100;
 maxZ = 100;
 
  var RandX = Random.Range(minX,maxX);
  var RandZ = Random.Range(minZ,maxZ);
  
 
 oHouse.transform.localScale.x = 7.0;
 oHouse.transform.localScale.y = 12.0;
 oHouse.transform.localScale.z = 7.0;
 
 oHouse.transform.position = Vector3(RandX,0,RandZ);
 
 
 oWindow.transform.localScale.x =1.1;
 oWindow.transform.localScale.y = 1.116753;
 oWindow.transform.localScale.z = 1.0;
 
 var wind_X = RandX + 2.82102;
 var wind_Z = RandZ + 3.014586;
 var Y = 5.118503;
 oWindow.transform.position = Vector3(wind_X, Y,wind_Z);
 
 oWindow.transform.renderer.material.color = Color.clear;

 oHouse.transform.parent = GameObject.Find("oWindow").transform;    
 
 var aTopWindows = new Array();
 var i: int;
 var nPosX: float;
 nPosX = wind_X - 2.1;
 
 for(i=0; i<=3; i++){
     if(oWindow != null) {//if the object oWindow exists 
         var cloneTopWindows = Instantiate(oWindow, Vector3(nPosX,Y,wind_Z), Quaternion.identity); //make 3 more clones of the oWindow object and place them near to it
         //aTopWindows.Push(cloneTopWind);
         //aTopWindows[i] = cloneTopWind;
         aTopWindows.Add(cloneTopWindows);
         nPosX-=2.1;
         //return aTopWindows[i];
         
     }
 }
 }

I want your comments about the way i'm using the Array, and why i can't see the (rest 3) window clones on the building's facade, after i instatiate the gameObject and add it in the array?

If you do have a better approach on that issue, please i'm all ears :) . Thanks in advance.

PS: plus i get the exception: "NullReferenceException House2.Start () (at Assets/House2.js:45)", and shows me the line: oHouse.transform.parent = GameObject.Find("oWindow").transform;

Comment
Add comment · Show 5
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 hijinxbassist · May 24, 2012 at 03:44 AM 0
Share

The reason you get a null is because you are searching the scene for something called oWindow, ins$$anonymous$$d you should just say

 oHouse.transform.parent = oWindow.transform;

since oWindow is right there in this script. Btw, if you look at the object in the scene view you will notice the name will be "Cube" since you did not manually rename it(but that doesnt matter anyway). So you can see the first window even though it is clear? have you checked in the scene view to see if the other windows are created, possibly all in the same place or in some obscure location.

avatar image hijinxbassist · May 24, 2012 at 04:01 AM 0
Share

Also, i see you are creating variables in start that dont need to be vars, like $$anonymous$$ and max(since you dont use them again..also you can declare their values in the same line as the declaration.

 var nPosX: float;
 nPosX = wind_X - 2.1;

can be written as

 var nPosX:float=wind_X-2.1;
avatar image velv · May 24, 2012 at 10:28 AM 0
Share

You quite right,hijinxbassist , about your 1st comment. Now i see the clone windows in parallel. I tried to set my vars $$anonymous$$, max outside the function Start(), but i get the exception: "ArgumentException: Range can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, ins$$anonymous$$d move initialization code to the Awake or Start function. House2..ctor () (at Assets/House2.js:11) " for the Random method. So i have to use it inside the method.

Also there is another problem co$$anonymous$$g out, the parent-child relationship between oHouse object and oWindow objects does not work. If i move the building the windows remain at the same position, not following the parent (oHouse). Plus, when the 1st (clone) window object appears, it appears with a parent clone of the oHouse object but the rest appear as a unique object. There is something wrong inside the for loop. Here are some changes:

for(i=0; i<=2; i++){ if(oWindow != null) {//if the object oWindow exists var cloneTopWindows = Instantiate(oWindow, Vector3(nPosX,wind_Y,wind_Z), Quaternion.identity); //make 2 more clones of the oWindow object and place them near to it aTopWindows.Add(cloneTopWindows); nPosX-=1.9; oHouse.transform.parent =cloneTopWindows.transform; } }

Have you got any idea? And again, thank you for making me moving another step forward with this.

avatar image hijinxbassist · May 24, 2012 at 05:58 PM 0
Share

The parent child relationship is backward. I wasnt sure if thats what you wanted or not. It should be

 oWindow.transform.parent=oHouse.transform;

Basically oWindows parent is the house, so you can move the window independently but if you move the house the windows come with. Im pretty sure thats why the one window created another instance of the house, since house was a child of oWindow.

For $$anonymous$$ and max, i was saying to get rid of them completely since you use them only once and they are just int s.

 RandX=Random.Range(-100,100);
 RandZ=Random.Range(-100,100);
avatar image velv · May 24, 2012 at 10:52 PM 0
Share

excellent! Thank you so much for your help. +1

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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 Clone Array1 into Array2 in Unity? Using JavaScript. 0 Answers

How to destroy the first clone of a UI Image using an array every time I press a button? 1 Answer

Array of prefabs 0 Answers

I want to add cloned gameobjects that are triggered, to an array. 2 Answers

Is there a way to pass an array inside a window? 0 Answers


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