- Home /
Parenting issue: Building and windows (scaling them as a whole entity)
Another question came about after i solved the main [problem][1] . Now i have to change (by code) the XYZ scale of my building (parent) and the windows i placed upon it (children) must change too according to the new scaling of their parent.
I checked here some parenting forum topics,but did not helped me much, cause the answers were for a specific question on parenting that someone had.
I am aware of the transform.parent.localScale but i do not know how to use it in my case. Here is the code:
function Start () {
//--Main Building Objects-----------------------------------------------------------
var oHouse = GameObject.CreatePrimitive(PrimitiveType.Cube);
var oWindow = GameObject.CreatePrimitive(PrimitiveType.Cube);
//----Start oHouse-------------------------------------------
//Random ranges between X, Y coordinates
var RandX = Random.Range(-100,100);
var RandZ = Random.Range(-100,100);
//Scaling the oHouse object
oHouse.transform.localScale.x = 7.0;
oHouse.transform.localScale.y = 12.0;
oHouse.transform.localScale.z = 7.0;
//Positioning the oHouse object
oHouse.transform.position = Vector3(RandX,6.35569,RandZ);
//----End oHouse-------------------------------------------
//----------------Start oWindow-------------------------------------
//Scaling the oWindow object
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_Y = 11.474193;
var wind_Z = RandZ + 3.014586;
//Positioning the oWindow object
oWindow.transform.position = Vector3(wind_X,wind_Y,wind_Z);
oWindow.transform.renderer.material.color = Color.clear; //transparency
oWindow.transform.parent = oHouse.transform; //make oWindow object child to oHouse object, parenting
var aTopWindows = new Array(); // an Array - a collection- for the top windows
var i: int;
var nPosX_TW: float = wind_X - 1.9;
for(i=0; i<=2; i++){
if(oWindow != null) {//if the object oWindow exists
var cloneTopWindows = Instantiate(oWindow, Vector3(nPosX_TW,wind_Y,wind_Z), Quaternion.identity); //make 3 more clones of the oWindow object and place them near to it
cloneTopWindows.transform.localScale.x =1.1;
cloneTopWindows.transform.localScale.y = 1.116753;
cloneTopWindows.transform.localScale.z = 1.0;
aTopWindows.Add(cloneTopWindows);
nPosX_TW-=1.9;
cloneTopWindows.transform.parent = oHouse.transform; //parenting
} //end if
} //end for
//----------------End oWindow-------------------------------------
} //End Start()
When i changed the scale coordinates of the oHouse object and play the scene, i saw a tiny box in the place of oHouse Building and the windows' scale was remained unchanged. Anyone knows why? Thanks in advance. [1]: http://answers.unity3d.com/questions/256544/placing-parallel-window-objects-into-a-building.html
Answer by whydoidoit · May 25, 2012 at 10:56 PM
Well you are setting a scale before setting transform.parent - that's one of those weird things that happen with ordering.
Basically when you set transform.parent it will keep the windows the same WORLD scale as they were before they had a parent. (Their localScale will change!) If you want to set them in proportion set their scaling after the transform.parent = statement.
If you add things to a parent, then scale the parent, the children scale automatically (that's why it's localScale).
If i'm following right ur instructions you said to use the transform.parent statement for each (child) object before i set their localScale statement, right? It didn't work. You can test it as well. But thank you helping me out.
Believe me I use that code everywhere, that ordering makes a difference.
You should be able to add normally sized windows to a normal sized house (by setting their transform.parent) then scale the whole unit by scaling the house. If the house is already scaled then you would have to scale the windows as well before you add them, or set their normal scale afterwards.