- Home /
modifying renderer.materials resets scaling of renderer.bounds until transform.position is updated
I have decided to rewrite this question to be short and concise. Check out the revision history to see the original question.
I noticed when using renderer.bounds.size to get the size of an object in World Space, if I alter renderer.materials, renderer.bounds.size does not return the correctly scaled bounds until I update transform.position. For example, given the following code:
Material[] newMaterialArray = new Material[subchild.renderer.materials.Length-1];
System.Array.Copy(subchild.renderer.materials,newMaterialArray,subchild.renderer.materials.Length-1);
subchild.renderer.materials = newMaterialArray;
Debug.Log(subchild.renderer.bounds.size);
If subchild has a lossyScale that is anything other than (1,1,1) , the debug call will return an incorrectly scaled value for renderer.bounds.size. However, if an additional line is added changing the value of transform.position such as as in the below example:
Material[] newMaterialArray = new Material[subchild.renderer.materials.Length-1];
System.Array.Copy(subchild.renderer.materials,newMaterialArray,subchild.renderer.materials.Length-1);
subchild.renderer.materials = newMaterialArray;
subchild.transform.position = subchild.transform.position;
Debug.Log(subchild.renderer.bounds.size);
the debug call will now return the correct value for renderer.bounds.size. Note that in this example I remove one material from the renderer.materials array, but I believe the same result can be found by adding a material to renderer.materials, or by replacing one of the existing materials in renderer.materials.
I have since modified the design of my project and now use mesh.bounds rather than renderer.bounds (mesh.bounds.size is always unscaled, so it does not suffer from this 'bug'). However, I am leaving this question up as I never was able to determine the reason why modifying renderer.materials resets the scaling of renderer.bounds in this way. I am currently using Unity 4. If anybody knows the cause of this behavior, please let me know. Also, feel free to reproduce this scenario and let me know if this still occurs in your own project or in the most recent version of Unity.
Your answer
Follow this Question
Related Questions
Procedural object generation, objects overlap each other 1 Answer
How to grab position and bounds of Trackables in real time (AR Foundation)? 0 Answers
Collider.bounds position is wrong? 3 Answers
BoundsInt.allPositionsWithin is not working? 1 Answer
How to get the center point of an object without any renderers? 2 Answers