Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Editor & General Support' started by DemonicOrigins, Aug 24, 2023.

  1. DemonicOrigins

    DemonicOrigins

    Joined:
    May 13, 2023
    Posts:
    2
    I got this error twice while trying to program a 2D game in Unity 2021.3.15f1. I'm new to using Unity, so I'm not too sure what it means or how to fix it. I did some research and did not find anything I think could solve this. This is the code for my player, error was on line 15:
    Code (csharp):
    1. public class Player : MonoBehaviour
    2. {
    3.  
    4.     Enemy enemy;
    5.     GameManagerScript gm;
    6.     public float turnSpeedStart;
    7.     public float turnSpeed;
    8.     public TMP_Text scoreDisplay;
    9.     public TMP_Text Timer;
    10.     int score;
    11.     public float time;
    12.  
    13.     void Update()
    14.     {
    15.         turnSpeed = turnSpeedStart + (time / gm.gameSpeed);
    16.         transform.Rotate(Vector3.back * turnSpeed * Input.GetAxisRaw("Horizontal")  * Time.deltaTime);
    17.  
    18.         time += Time.deltaTime;
    19.         Timer.text = time.ToString("0.0");
    20.     }
    If there is more information I need to provide, please lmk.
     
  2. TheNullReference

    TheNullReference

    Joined:
    Nov 30, 2018
    Posts:
    222
    Your Enemy and GameManagerScript have not been set to an instance of those objects.

    In C#, fields aren't automatically assigned.

    You need to add the public keyword Infront of those two fields, and drag and drop the respective monobehaviours into those fields.

    If they're not monobehaviours, then you need to write Enemy enemy = new Enemy()
     
  3. DemonicOrigins

    DemonicOrigins

    Joined:
    May 13, 2023
    Posts:
    2
    That worked with most of my assets, the only exception is the prefab. For my enemy, I did that exact same thing before making it a prefab because I wasn't able to add anything while it was a prefab, and that worked. Then I ran the program and after getting an error when the first enemy spawned, I stopped the program. I looked at the assigned assets, and it said, "None (Player)" and "None (Game Manager Script)". Did I mess something up, or is there a completely different thing I need to do for instantiated prefabs?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
  5. TheNullReference

    TheNullReference

    Joined:
    Nov 30, 2018
    Posts:
    222
    Prefabs are treated differently than normal unity objects. A prefab is more like a blueprint for something you would like to create at runtime.

    Code (csharp):
    1.  
    2. public class EnemySpawner : MonoBehaviour
    3. {
    4.  
    5.     public Enemy EnemyPrefab;
    6.  
    7.     private void SpawnEnemy()
    8.     {
    9.         var newEnemy = Instantiate(EnemyPrefab, this);
    10.     }
    11. }
    12.  
    In this example, we use the prefab to create new instances of the Enemy. There are some ways you can deal with getting a reference to that enemy.

    Code (csharp):
    1.  
    2. public class EnemySpawner : MonoBehaviour
    3. {
    4.     [SerializedField]
    5.     private Enemy EnemyPrefab; // Keep this private because we don't wont other classes accessing it.
    6.    
    7.     // You can get a reference to all the enemies in a list, just make sure to remove them when they die.
    8.     public List<Enemy> AllEnemies = new();
    9.     // If you only wanted the last spawned enemy you could this reference. Be aware that it would change often!
    10.     public Enemy lastSpawnedEnemy;
    11.     // We could also broadcast an event when a new enemy is spawned as well.
    12.     public event Action<Enemy> newEnemySpawned;
    13.  
    14.     private void SpawnEnemy()
    15.     {
    16.         var newEnemy = Instantiate(EnemyPrefab, this);
    17.         AllEnemies.Add(newEnemy);
    18.         lastSpawnedEnemy = newEnemy;
    19.         newEnemySpawned?.Invole(newEnemy);
    20.     }
    21. }
    22.