Search Unity

Question Object Reference not set to an instance

Discussion in 'Scripting' started by unity_xXyJqtEjf2yZLQ, Dec 6, 2022.

  1. unity_xXyJqtEjf2yZLQ

    unity_xXyJqtEjf2yZLQ

    Joined:
    Feb 7, 2022
    Posts:
    5
    I got a unity project with multiple scenes, and I got it to pretty much it just plays until I call it quit. However, after one loop (game menu, then game play, then game over (which is where I save the score of the player)), when I try to go the game play scene again, I got the "Object reference not set to an instance of an object". Does anyone know why?

    This is where it led
    Code (CSharp):
    1. foreach(Enemy enemy in enemyScripts)
    2.         {
    3.             // If the script object is not null, its game object is still in the scene.
    4.             if (enemy != null)
    5.             {
    6.                 // Tell the enemy to perform its movement behavior.
    7.                 enemy.Move(player.transform);
    8.  
    9.                 // Add the current script to the new script list.
    10.                 newScripts.Add(enemy);
    11.             }
    12.         }
    me to:
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
  3. unity_xXyJqtEjf2yZLQ

    unity_xXyJqtEjf2yZLQ

    Joined:
    Feb 7, 2022
    Posts:
    5
    So I have fiddle around with it, and it seems that when I tried to go to the play scene again, for some reason, despite that the player game object is linked to the enemy manager, the enemy manager still could not see the player.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Make sure you're doing that correctly. No asset (such as a prefab) on disk can hold a reference to anything in the scene... only the other way around.