Search code examples
c#unity-game-engineidictionary

NullReferenceException When Assigning Key Value Pair in Unity


I am trying to develop a stat assignment system in Unity3D using the UI functions, but I am running into a issue where when I attempt to assign a Key Value pair to an IDictionary the Unity Console throws the following error

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

The following is the corresponding script throwing the error:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class character_Creation : MonoBehaviour

{

    public InputField characterName;
    public Dropdown stats;
    public Dropdown statSelector;
    public Dropdown raceSelection;
    public Dropdown classSelection;
    public Dropdown alignment;
    public Button submit;
    public Button apply;
    public GameObject menu;
    public GameObject statCreation;
    private IDictionary<int, float> list;
    private float totalRoll;
    private float stat1;
    private float stat2;
    private float stat3;
    private float stat4;
    private float stat5;
    private float stat6;

    public void Awake()
    {
        submit.onClick.AddListener(submitButton);
        apply.onClick.AddListener(applyStat);
    }

    public void submitButton()
    {
        string name = characterName.text;
        string race = raceSelection.options[raceSelection.value].text;
        string myClass = classSelection.options[classSelection.value].text;
        string align = alignment.options[alignment.value].text;

        Debug.Log(name);
        Debug.Log(race);
        Debug.Log(myClass);
        Debug.Log(align);
        Debug.Log("Rolling Character Stats!");
        list.Add(0, diceRoll(6, 3));
        list.Add(1, stat2 = diceRoll(6, 3));
        list.Add(2, stat3 = diceRoll(6, 3));
        list.Add(3, stat4 = diceRoll(6, 3));
        list.Add(4, stat5 = diceRoll(6, 3));
        list.Add(5, stat6 = diceRoll(6, 3));
        PopulateDropdown(stats);
        menu.SetActive(false);
        statCreation.SetActive(true);



    }
    public void applyStat()
    {
        list.Remove(0);
    }

    public float diceRoll(int type, int number)
    {
        totalRoll = 0;
        while (number >= 0)
        {
            float roll = Random.Range(1, type);
            totalRoll += roll;
            number += -1;
        }

        return totalRoll;
    }

    public void PopulateDropdown(Dropdown dropdown)
    {
        List<string> options = new List<string>();
        foreach (KeyValuePair<int, float> option in list)
        {
            options.Add(option.Value.ToString());
        }

        dropdown.ClearOptions();
        dropdown.AddOptions(options);
    }
}

Basically I am unable to add my individual stats into my Dictionary so I can later populate a Dropdown menu with said stats.

Thanks in Advance for the assistance

Problem was solved by converting IDictionary to Dictionary and actually assigning the variable list as a Dictionary.

list = new Dictionary<int, float>();

Solution

  • It does not appear you are ever assigning an object to your "list" variable (i.e. list = new Dictionary<int, float>();).

    The statement private IDictionary<int, float> list; only declares a variable called "list"; it does not actually assign a value to it.