Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kala476 · May 04, 2019 at 02:53 PM · spritessprite renderer

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fedemg15 · Jun 14, 2021 at 05:25 PM 0
Share

Hey, I'm going through something similar, did you find a workaround?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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...

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image andrew-lukasik · Apr 07 at 09:19 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

110 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Translucent sprites appear solid 0 Answers

Does anyone know why my sprite isn't showing up? 2 Answers

Rendering Transparent Geometry Performance Issue 0 Answers

Semi-Transparent Sprite over another Sprite? 1 Answer

Sorting Layers in a 3D Environment 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges