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(
                new Vector3(42.34f, -5.14f, 0),
                new Vector3(42.34f + (2 * bounds.extents.x), -5.14f, 0),

        void OnDrawGizmos() {
                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:


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


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?

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.