Suppose I have a reference GameObject, Cube, with scale (1,1,1). There is another GameObject with scale (1,1,1), but the size is 3 times bigger than the Cube. How do I dynamically change the scale of the Game objects to fit the size of the Cube?
1 Answer
\$\begingroup\$
\$\endgroup\$
Untested, and completely from memory, but something kind of along these lines should work. I'll test it tonight when I have access to Unity.
//scales all GameObjects in goResizeList to have the same size as the referenceGO.
public void resizeAll(List<GameObject> goResizeList, GameObject referenceGO){
Vector3 refSize = referenceGo.renderer.bounds.size;
foreach(GameObject go in goResizeList){
float resizeX = refSize.x / go.renderer.bounds.size.x;
float resizeY = refSize.y / go.renderer.bounds.size.y;
float resizeZ = refSize.z / go.renderer.bounds.size.z;
resizeX *= go.transform.localScale.x;
resizeY *= go.transform.localScale.y;
resizeZ *= go.transform.localScale.z;
go.transform.localScale = new Vector3(resizeX, resizeY, resizeZ);
}
}
renderer.bounds.sizeis equal to the cube? \$\endgroup\$