- Home /
bounds.SetMinMax does not do anything
I have an object with just a sprite renderer on It. I need to be able to set the position of the corners of the sprite from code manually.
I have an object with just sprite renderer on it. The debug shows me the correct positiions of the bounding box corner. Why does none of the code in update change anything? I am getting crazy with this one.
public class wtfbounds : MonoBehaviour
{
Sprite sprt;
SpriteRenderer sprtRen;
// Start is called before the first frame update
void Start()
{
sprtRen = this.GetComponent<SpriteRenderer>();
sprt = sprtRen.sprite;
Debug.Log(string.Format("Rendere: Bounds: {0} extenst {1} min: {2} max: {3} ", sprtRen.bounds, sprtRen.bounds.extents, sprtRen.bounds.min, sprtRen.bounds.max));
Debug.Log(string.Format("Sprite: Bounds: {0} extenst {1} min: {2} max: {3} ", sprt.bounds, sprt.bounds.extents, sprt.bounds.min, sprt.bounds.max));
}
// Update is called once per frame
void Update()
{
if(Input.GetKey(KeyCode.Space)){
// option 1
sprtRen.bounds.SetMinMax(new Vector3(1f, 1f, 0f), new Vector3(-1f, -1f, 0f));
// option 2
sprt.bounds.SetMinMax(new Vector3(1f, 1f, 0f), new Vector3(-1f, -1f, 0f));
// option 3
Bounds sprtRenbounds = sprtRen.bounds;
sprtRenbounds.min = new Vector3(1f, 1f, 0f);
sprtRenbounds.max = new Vector3(-1f, -1f, 0f);
Debug.Log(string.Format("Rendere: Bounds: {0} extenst {1} min: {2} max: {3} ", sprtRen.bounds, sprtRen.bounds.extents, sprtRen.bounds.min, sprtRen.bounds.max));
Debug.Log(string.Format("Sprite: Bounds: {0} extenst {1} min: {2} max: {3} ", sprt.bounds, sprt.bounds.extents, sprt.bounds.min, sprt.bounds.max));
}
}
}
I am Using unity 2019.1.0. This is the only script in the sceene.
Hey, I'm going through something similar, did you find a workaround?
Answer by WolfVD · Apr 07 at 08:33 AM
Not exactly the same problem but I ran into something similar (and spent an embarrassing amount of time debugging) so wanted to share so other people didn't run into the same problem.
If you have a Bounds property Bounds bounds { get; set; }
and you use bounds.SetMinMax(min, max)
, that won't do anything.
This is because structs are pass-by-value. When you type bounds
, it creates a copy of the underlying variable. Thus, when you use .SetMinMax
, you're setting the min and max on the new struct without actually modifying the bounds
variable
It's a C# quirk that you can't do much about...
True. But it's not a quirk necessarily but it's because default get;
property accessor implementation in c#, like this Bounds bounds { get; }
, is a shorthand for Bounds bounds_get () { return _bounds; }
- basically a method that returns you a struct copy.
This is exactly why you don't write transform.position.Set( 1 , 2 , 3 );
but transform.position = new Vector3( 1 , 2 , 3 );
elsewhere in your code.
Your answer

Follow this Question
Related Questions
Sprite managed by Sprite Renderer doesn't shows up in play mode 0 Answers
Dynamic texture resize 0 Answers
How do I partially repaint sprite? 0 Answers
Translucent sprites appear solid 0 Answers