Problem referencing an int inside of a dictionary.
So I have a dictionary set up to contain two variables, a string indicating the name of the object, and an int indicating how many of that object the player has. I'm trying to get this script to activate a button when the player has an item, and then display how many of that item the player has.
public void ServerCardAddCount(string GPUname, GameObject Button)
{
if (Inventory.GPUCountDic[GPUname] <= 0)
{
Button.GetComponent<Button>().interactable = false;
}
else
{
Button.GetComponent<Button>().interactable = true;
TenEightyAddButton.GetComponent<Text>().text = "Add 1080: " + Inventory.GPUCountDic["GTX 1080"] + "in inventory";
TenSeventyAddButton.GetComponent<Text>().text = "Add 1070: " + Inventory.GPUCountDic["GTX 1070"] + "in inventory";
TenSixtyAddButton.GetComponent<Text>().text = "Add 1060: " + Inventory.GPUCountDic["GTX 1060"] + "in inventory";
}
The button activates and deacivates based on how if the player has that item just fine, but when the button is set to true, the console spits out
NullReferenceException: Object reference not set to an instance of an object
And then points me to the first line of code displaying the object count.
The dictionary is created in the Start() function of another script.
{
public static Dictionary<string, int> GPUCountDic = new Dictionary<string, int>();
public void Start()
{
GPUCountDic.Add("GTX 1080", 0);
GPUCountDic.Add("GTX 1070", 0);
GPUCountDic.Add("GTX 1060", 0);
}
Any help would be awesome.
When you are declaring your variables in your script, how are you declaring:
TenEitghtyAddButton
TenSeventyAddButton
TenSixtyAddButton
Additionally, do you have those buttons set in your Inspector? Its possible you forgot to set those button objects in your inspector.
They are all public GameObjects and it's all correctly set up in the inspector. I've done all the normal sanity checks
Your answer
Follow this Question
Related Questions
Rotation Changing Z-Coordinate Value 0 Answers
NullReferenceException: Object reference not set to an instance of an object, again 2 Answers
Controller gives NullReferenceException and Teleporter is unresponsive when collided 1 Answer
Getting "object reference not set" error when creating a list dictionary. 1 Answer
Varialbes on prefab script always null 0 Answers