- Home /
Matching an object's size
Hi, I'm trying to scale an object (with a rigidbody) to match the exact size of another object (with no rigidbody, just a mesh renderer).
I have tried collider.bounds.size = object2.renderer.bounds.size;
which gives me the error CS1612: Cannot modify a value type return value of `UnityEngine.Collider.bounds'. Consider storing the value in a temporary variable
I looked up this error and tried this:
Vector3 temp = collider.bounds.size;
temp.x = object2.renderer.bounds.size.x;
temp.y = object2.renderer.bounds.size.y;
collider.bounds.size = temp;
but I think I'm doing something stupid here because I still get the same error.
Anyone know how I can do this please?
I don't know about your specific problem, but to get two game object to be the same size:
transform.localScale = goOther.transform.localScale;
That gives the object the same scale but not the same size - they're both still different heights and lengths as they were different sizes originally.
Does it need to be absolutely exact? or can it be off by a few decimal places?
If it doesn't need to be fully exact you should be able to do the following: set one of their localScales to 1 then scale the other and make a note of it's scale values in each axis. These scale values will become your scaleFactors.
Then do something like:
transform.localScale = new Vector3(other.transform.localScale.x * scaleFactorX, other.transform.localScale.y * scaleFactorY, other.transform.localScale.z * scaleFactorZ);
Great idea but this seems to make the objects disappear entirely. I tried local scale at first and I've now tried render.bounds as it's a 3D text object but his doesn't seem to work either:
transform.localScale = new Vector3(object2.renderer.bounds.size.x scaleFactorX, object2.renderer.bounds.size.y scaleFactorY, object2.renderer.bounds.size.z * scaleFactorZ);
Answer by Immanuel-Scholz · Jun 09, 2013 at 10:12 AM
The error is generated when you try to modify a "value type", which in this particular cas means a struct.
When a function or property return a value types (as structs are), you always get a copy instead of the original. So changing that copy would not make sense, because you want to change the original bounds of the renderer - not just a copy.
The struct that the compiler is complaining about are "bounds", not "size". (Although size is a vector, which is an struct too)
So when you do collider.bounds.size = object2.renderer.bounds.size
you are assigning a new size to a copy of the colliders bounds. Which is most definetely not what you would expect. C# emits an error for that.
If at all, you would have to assign the bounds - property itself.
// doesn't work either
collider.bounds = new Bounds(collider.bounds.center, object2.renderer.bounds.size);
But then you will get another error, namely that "bounds" is a read-only property.
To change collider sizes, you can either use some specific collider functions (e.g. if you are using a CapsuleCollider, it has some properties as "height", "radius" and "center"). Or you can change the transform - component that the collider is attached to, which is also used by the collider itself.
// changes the scale of the transform, which is also used by the collider
transform.localScale = object2.renderer.bounds.size
Excellent answer. OP -- of course, you can not modify bounds.
Thanks for your detailed answer, Immanuel. This works when I try it on 2 cubes but object2 is a 3D text object and this doesn't scale the cube to match it correctly - it's scaled perfectly in X but in Y is about 3 times too big.
Your answer
Follow this Question
Related Questions
Can I use a raycast to retrieve information from the collider? 2 Answers
Changing the size of a text object to only be as long as the letters 0 Answers
Percentage of Collider within a Trigger Area (C#) 1 Answer
Can you scale a Mesh independently of it's Collider? 1 Answer
Converting Bounds.size to GUI? 0 Answers