Search Unity

Null reference on first Instantiate

Discussion in 'Scripting' started by Coldspell, May 16, 2019.

  1. Coldspell

    Coldspell

    Joined:
    May 5, 2014
    Posts:
    1
    So here is my problem...

    I have a scene with a GameObject which has a script that causes it to instantiate a GameObject that draws parameters from random scriptableobjects every second. The objects destroy themselves shortly after.

    Now when I test the scene everything looks perfect. The objects spawn just fine, and I get ZERO errors.

    While testing however, if I navigate to a new scene, then BACK to the Original scene with the Spawner, the very first object spawn comes back missing the reference to the scriptableobject. every object after that works perfectly.

    But that first object gives me

    NullReferenceException: Object reference not set to an instance of an object,

    which stacks every frame until the game object destroys itself. The Spawner is on Update()!

    Anyone have any ideas what could be causing this to happen?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Without seeing some code and understanding how you have things setup in your scene, it's difficult to say.
     
  3. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Without code we're just guessing. So here's my guess: You are using an Object with Don'tDestroyOnLoad that references an object that does not have that flag set --> boom.