Ill be the first to admit, Im not fluent in C#, but I started this project for school and I have 2 scripts; Game_Handler, and Game_Set_Handler. Game_Handler does the actual game while the Set_Handler handles my dictionary.
Heres Game_Set_Handler
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using UnityEngine.UI;
using System.Threading.Tasks;
using System.Diagnostics;
using Random = System.Random;
public class Game_Set_Handler : MonoBehaviour
{
public class Set
{
public string? Question { get; set; }
public int StartingPoints { get; set; }
public string Answer1 { get; set; }
public string Answer2 { get; set; }
public string Answer3 { get; set; }
public string? Answer4 { get; set; }
public string? Answer5 { get; set; }
}
public Dictionary<int, Set> Sets;
public void Handler()
{
Sets = new Dictionary<int, Set>()
{
{1,new Set { Question="sports to play outside", StartingPoints=250, Answer1 = "football", Answer2 = "baseball", Answer3 = "basketball", Answer4 = "soccer", Answer5 = "golf"}},
{2,new Set { Question="board games", StartingPoints=375, Answer1 = "chess", Answer2 = "checkers", Answer3 = "monopoly",Answer4 = "battleship",Answer5 = "mancala"}},
{3,new Set { Question="famous religions", StartingPoints=400, Answer1 = "christianity", Answer2 = "islam", Answer3 = "judaism",Answer4 = "buddhism",Answer5 = "hinduism"}},
{4,new Set { Question="types of bread in the USA", StartingPoints=225, Answer1 = "banana", Answer2 = "sourdough", Answer3 = "white",Answer4 = "wheat",Answer5 = "pumpkin"}},
{5,new Set { Question="car brands in the USA", StartingPoints=100, Answer1 = "ford", Answer2 = "chevrolet", Answer3 = "toyota",Answer4 = "honda",Answer5 = "volkswagen"}},
};
}
void Start()
{
Handler();
}
public int Get_Question()
{
int value = new Random().Next(0, Sets.Count + 1);
return value;
}
}
And here is how I link the two (this is Game_Handler):
var dictionary = Parent.GetComponent<Game_Set_Handler>().Sets; //Parent is the Canvas, both scripts are connected to it
int dict_key = Parent.GetComponent<Game_Set_Handler>().Get_Question();
It works absolutely fine, until I've tested the Unity project 6 or 7 times, then it throws this error every-time I test it afterwards:
Its worth noting that if I copy the same exact script, make a new script, and paste it(obviously changing the class name), it works just fine until I test it 6 or 7 times.
NullReferenceException: Object reference not set to an instance of an object
Game_Set_Handler.Get_Question () (at Assets/Scripts/Game_Set_Handler.cs:53)
Game_Handler.Start_Game () (at Assets/Scripts/Game_Handler.cs:60)
Game_Handler.Start () (at Assets/Scripts/Game_Handler.cs:121)
Ive been stuck on this for the last 2 days and I'm about to take the L and move on, if anyone could help I would appreciate it.
I've tried replacing the function with a different one but the error still proceeds, I've copied it 3 times over just for testing purposes but I feel as if when I export it into an executable, it will happen then aswell.