Object Reference Not Set To Instance

I need help figuring out what’s going on. This code was working fine, then I started another script, maybe I changed this one I don’t remember, but now it’s not working. I deleted the new script and put this one back to the best of my ability, but I’m still getting the “NullReferenceException: Object reference not set to an instance of an object” error. The error is on the line with "person.Add(person0) I think. The public GameObjects all have sprites within the inspector. Here’s the code:

public class ChallengesMain : MonoBehaviour
{

    public int numOfChallenges;

    int challengeNum;

    public static List<GameObject> person;

    public GameObject person0;
    public GameObject person1;
    public GameObject person2;
    public GameObject person3;
    public GameObject person4;
    public GameObject person5;
    public GameObject person6;
    public GameObject person7;
    public GameObject person8;
    public GameObject person9;
    public GameObject person10;
    public GameObject person11;

    public GameObject dumbLuck;

    // Start is called before the first frame update

    void Start()
    {
        System.Random r = new System.Random();
        challengeNum = r.Next(0, numOfChallenges);

        person.Add(person0);
        person.Add(person1);
        person.Add(person2);
        person.Add(person3);
        person.Add(person4);
        person.Add(person5);
        person.Add(person6);
        person.Add(person7);
        person.Add(person8);
        person.Add(person9);
        person.Add(person10);
        person.Add(person11);

}

This is not code. It is text. Please edit your post and put your code in code tags like it is described here:
https://discussions.unity.com/t/481379

Why do you need person0 … personN if you just put them in a List? You can see the list in the inspector too if it is not static. And it really should not be static.
You can populate it there.

You never initialize this member. You’re missing a “new List();” somewhere before you call the first “person.Add”.

2 Likes

Some notes on how to fix a NullReferenceException in Unity3D (also Unassigned Reference errors):

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

The basic steps outlined above are:

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