MissingReferenceException: The object of type 'SpriteRenderer' has been destroyed but you are still (859105)

as mentioned in the title, I get error message when transitioning from one scene to another:

MissingReferenceException: The object of type ‘SpriteRenderer’ has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.

I have a character empty object, that has several objects within that has sprite renderer: example with screen shot:
Note: these are not the exact names, just to help with explanation
Male_Empty_GameObject
— Hair - Game Object with Sprite Renderer
— Shirt - Game Object with Sprite Renderer
---- Pants - Game Object with Sprite Renderer

I tried to use the following:

        public void DestroyObjects()
        {
            DontDestroyOnLoad(PlayFabController.PFC.RootPlayerCharacter);

            DontDestroyOnLoad(PlayFabController.PFC.Male_char);
            DontDestroyOnLoad(PlayFabController.PFC.Female_char);
            DontDestroyOnLoad(PlayFabController.PFC.f_hair);
            DontDestroyOnLoad(PlayFabController.PFC.f_skin);
            DontDestroyOnLoad(PlayFabController.PFC.f_shirt);
            DontDestroyOnLoad(PlayFabController.PFC.f_pants);
            DontDestroyOnLoad(PlayFabController.PFC.m_hair);
            DontDestroyOnLoad(PlayFabController.PFC.m_skin);
            DontDestroyOnLoad(PlayFabController.PFC.m_pants);
            DontDestroyOnLoad(PlayFabController.PFC.m_shirt);

           
            //DontDestroyOnLoad(PlayFabController.PFC.m_shirt);
        }

unfortunately, it gives me this message:

DontDestroyOnLoad only works for root GameObjects or components on root GameObjects.

then spams message:
MissingReferenceException: The object of type ‘SpriteRenderer’ has been destroyed but you are still

not sure what to do exactly, my scene goes when player clicks button “Start Game” thats when the error comes up, with a new scene… I was thinking of just putting a new Panel for the levels… but not sure if that would be clean or not :frowning: thoughts and how to resolve this?

SWEET!!! nevermind, i found the problem, so I was calling or updating the sprite color and also the sprite image on the Update() method… i took that part out, worked in some logic… good to go :slight_smile: hope this helps someone in the future :slight_smile:

Awesome… congrats! This super-common problem most often happens when you have things of different lifecycles. For instance, lets say you have two buttons:

JUMP

DESTROY

If you press JUMP the object jumps

If you press DESTROY it is destroyed

But the buttons still stay (such as the stuff above marked as DontDestroyOnLoad())

This means if you poke JUMP then you’d be accessing the destroyed object.

ANYWAY… like I said, SUPER common error, and congrats on ripping through and fixing it. In the future, here is my go-to list of how to tackle stuff like this in three easy steps.

BEGIN BLURB:

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception
  • also known as: Object reference not set to an instance of an object

http://plbm.com/?p=221

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

You need to figure out HOW that variable is supposed to get its initial value. There are many ways in Unity. In order of likelihood, it might be ONE of the following:

  • drag it in using the inspector
  • code inside this script initializes it
  • some OTHER external code initializes it
  • ? something else?

This is the kind of mindset and thinking process you need to bring to this problem:

https://discussions.unity.com/t/814091/4

Step by step, break it down, find the problem.

1 Like