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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Simple Code - Stuck with "Code reference not set..."

Discussion in 'Scripting' started by TheRaziel, Mar 1, 2021.

  1. TheRaziel

    TheRaziel

    Joined:
    May 5, 2020
    Posts:
    2
    Hello,

    I'm currently going through the Unity Junior Developer pathway and I'm debugging an app. I can't for the life of me figure out why there is an issue with the list.

    Error:
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. CongratScript.Start () (at Assets/CongratScript.cs:25)


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CongratScript : MonoBehaviour
    6. {
    7.     public TextMesh Text;
    8.     public ParticleSystem SparksParticles;
    9.    
    10.     private List<string> TextToDisplay;
    11.    
    12.     private float RotatingSpeed;
    13.     private float TimeToNextText;
    14.  
    15.     private int CurrentText;
    16.    
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         TimeToNextText = 0.0f;
    21.         CurrentText = 0;
    22.        
    23.         RotatingSpeed = 1.0f;
    24.  
    25.         TextToDisplay.Add("Congratulation");
    26.         TextToDisplay.Add("All Errors Fixed");
    27.  
    28.         Text.text = TextToDisplay[0];
    29.        
    30.         SparksParticles.Play();
    31.     }
    32.  
    33.     // Update is called once per frame
    34.     void Update()
    35.     {
    36.         TimeToNextText += Time.deltaTime;
    37.  
    38.         if (TimeToNextText > 1.5f)
    39.         {
    40.             TimeToNextText = 0.0f;
    41.            
    42.             CurrentText++;
    43.             if (CurrentText >= TextToDisplay.Count)
    44.             {
    45.                 CurrentText = 0;
    46.  
    47.  
    48.                 Text.text = TextToDisplay[CurrentText];
    49.             }
    50.         }
    51.     }
    52. }
    There is more but I'm doing the best I can to be independant on this. Any help would be much appreciated.

    Thank you!
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
    You never assigned a value to your List variable. Since List is what's known as a "reference type" (as all classes in C# are), a variable of type List is actually just like a signpost that can point to a List instance. However, the signpost is always initially pointing to nothing. If you try to call a method on a reference that refers to nothing (null), you get a NullReferenceException.

    The solution is to point your reference at an actual instance of type List. To do this, you can create a new List object with a call to the constructor for List:

    Code (csharp):
    1. private List<string> TextToDisplay = new List<string>();
     
    TheRaziel and Brathnann like this.
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Well, you declare TextToDisplay, but you never initialize it. Then in Start you try to add values to it. So change line 10 to.

    Code (CSharp):
    1. private List<string> TextToDisplay = new List<string>();
    @PraetorBlue was a bit faster.
     
    TheRaziel and PraetorBlue like this.
  4. TheRaziel

    TheRaziel

    Joined:
    May 5, 2020
    Posts:
    2
    You're both brilliant, thank you very much :)
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,779
    To save you a lot of time in the future, remember ...

    The answer is always the same... ALWAYS. It is the single most common error ever.

    Don't waste your life spinning around and round on this error. Instead, learn how to fix it fast... it's EASY!!

    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.

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

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

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