2
\$\begingroup\$

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?

\$\endgroup\$
3
  • \$\begingroup\$ Is something like renderer.bounds.size good enough for you? \$\endgroup\$
    – Zibelas
    Commented Oct 12, 2020 at 21:45
  • \$\begingroup\$ yeah, that's good! so should I keep reducing the scale such that renderer.bounds.size is equal to the cube? \$\endgroup\$
    – MrRobot9
    Commented Oct 12, 2020 at 21:50
  • \$\begingroup\$ You should be able to calculate how much bigger/ smaller the second object is. Get the volumen from the reference object and the new object, divide them by each other and multiply your scale by the result. Should scale your second GO to a matching size. \$\endgroup\$
    – Zibelas
    Commented Oct 13, 2020 at 6:55

1 Answer 1

3
\$\begingroup\$

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);
  }

}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .