- Home /
Issue with bounds.SetMinMax assigning incorrectly.
Hello everyone! I hope your month is going well!
Currently I've run into this issue that I'm trying to work out. All the actual values themselves have been checked and rechecked, so it's not a careless assignment error on my part.
Given the following information:
GameObject go = new GameObject();
BoxCollider bc = go.AddComponent("BoxCollider") as BoxCollider;
Vector3 temp1 = new Vector3(nodeList[iterator].min.x, nodeList[iterator].min.y, nodeList[iterator].min.z);
Vector3 temp2 = new Vector3(nodeList[iterator].max.x, nodeList[iterator].max.y, nodeList[iterator].max.z);
bc.bounds.SetMinMax(temp1, temp2);
bc.center = new Vector3(nodeList[iterator].center.x, nodeList[iterator].center.y, nodeList[iterator].center.z);
Now, from here, if I print out the values of "nodeList[iterator].center.x" and "bc.center.x", they are identical.
If I print out the values of "nodeList[iterator].min.x" and "temp1.x" they are identical.
However... printing out "temp1.x" and "bc.bounds.min.x" gives me two different values! For instance, temp1.x might report as 8.333334, and bc.bounds.min.x reports as 8.25.
The next set might print out as temp1.x being 7.5 with bc.bounds.min.x being 7.416667.
I just cannot seem to figure out why there is this assignment issue happening with the bounds.SetMinMax(). The values are absolutely identical in all ways, and assignment works in everywhere else, but just specifically when assigning the value to the bounds.SetMinMax() it gives a clearly different value than what I am assigning to it.
Has anyone else ran into this problem, or know of why Unity's bounds.SetMinMax() function is doing this? Thanks a lot!
UPDATE: I have tried doing the SetMinMax() assignment directly through the GameObject itself that the BoxCollider is attached to, and this is having the same issue. GameObject.Collider.Bounds.SetMinMax(temp1, temp2), then, afterwards, GameObject.Collider.Bounds.Min.x IS NOT temp1.x -- as I can't actually get into Unity to see how the SetMinMax() function works and does assignment to the object, I can't decide whether this is a bug in Unity's engine, or not. If it is a bug, I'm not sure if I should report it here or other places.
I have a similar problem. I have created a Bounds
object as a member field to a class and declared it readonly
(this is in C#). I cannot change the values of the Bounds
object at all. If I remove the readonly
declaration I can then set the values of the Bounds
object!?! Clearly Unity does some sort of funky transformations to the CIL that cause this strange behaviour.
Answer by Inhuman Games · Jul 17, 2015 at 02:47 PM
Bounds is a struct.
In C#, structs are passed around by value. Collider.bounds will return a copy of the real bounds. Any changes to the copy will be not reflected in the original Collider.
Unforunately, Collider's bounds property only has a getter, not a setter. This means you can't change the copy and give it back to the collider.
The solution is to use BoxCollider's set properties:
BoxCollider boxCollider = GetComponent<BoxCollider>();
Bounds bounds = new Bounds();
bounds.SetMinMax(temp1, temp2);
boxCollider.center = bounds.center;
boxCollider.size = bounds.size;
Your answer
![](https://koobas.hobune.stream/wayback/20220613104436im_/https://answers.unity.com/themes/thub/images/avi.jpg)