Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 hjc_ · Apr 29, 2020 at 11:42 PM · spritespriterendererbounds

SpriteRenderer Bounds is Returning the Wrong Extents (Sometimes)

Hey all, I'm having the craziest behavior when trying to calculate the extents for a sprite. Basically, Unity will non-deterministically return a bad value when I run the game and other times it won't. Before I show images, here is my stripped down code:

 namespace Game.Player {
     public class PlayerClass : MonoBehaviour {
         protected Bounds bounds;
         protected GameObject prefab;
         protected GameObject inst;
         private void Awake() {
             bounds = prefab.GetComponent<SpriteRenderer>().bounds;
             var container = new GameObject("Container");
 
             inst = Instantiate(
                 prefab,
                 new Vector3(42.34f, -5.14f, 0),
                 Quaternion.identity
             );
             Instantiate(
                 preab,
                 new Vector3(42.34f + (2 * bounds.extents.x), -5.14f, 0),
                 Quaternion.identity
             );
         }
 
         void OnDrawGizmos() {
             Gizmos.DrawCube(
                 inst.GetComponent<SpriteRenderer>().bounds.center,
                 new Vector3(bounds.extents.x * 2f, bounds.extents.y, 0f)
             )
         }
     }
 }

Basically, take my prefab (which is a rectangle), put it in a spot and then put another instance of it 2 * extents.x away (and keep the y coords), with no rotation. AFAIU, this should put them right by each other (please correct me if I'm wrong).

Then in DrawGizmos we grab the center of that first prefab and manually draw a cube representing what the full extents are. Centered at the first prefab with width of 2 * extents.x and height of extents.y (this is mostly so it's easier to see the error on the x extents).

Here's what it looks like most of the time:

alt text

But every now in then (in this case, after playing and stopping the game about 6 times), it looks right, like this:

alt text

AFAICT, this is literally random, though it tends to be wrong more often than right. I do have a video if anyone wants that, but it's just what I explained above.

Any thoughts?

bad-extent.png (2.8 kB)
good-extent.png (2.4 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by hjc_ · Apr 30, 2020 at 06:26 PM

Ahhh!!! Finally figured it out. In my code, I had left in a line that rotated the prefab I was using for sizing before instantiating it (it was just forgotten). Something like:

 m_AssetPrefab.transform.rotation = someQuaternion;

This apparently persist permanently after the game finishes running. So I would think my assets would be rotated facing the positive x direction (0 degree) when the game booted, but half the time they'd be rotated 90 degrees or 127 or whatever, and reading the extents went "wrong". Unity was doing everything right, I just kept changing the sprite underneath it in between runs. Oops...

A good way to detect this is if your .prefab file changes in between each game run, you might have this bug.

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 hjc_ · Apr 30, 2020 at 08:55 PM 0
Share

Nope, apparently this isn't the full solution because Unity is still sometimes returning extents that are 100% wrong on all accounts. Very frustrating.

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

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

How to get the bounds of an actual sprite, not it's bounding box. 1 Answer

White lines between adjacent/overlapping sprites and from tiled sprites? 0 Answers

Distort shader not showing sprites 2 Answers

How come setting sprite tint/alpha does not increase draw calls? 0 Answers

Get Sprite index in array by filename 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