,Trouble with Dictionary and displaying text. Code not starting from top?
Hello! I am having an issue with adding items to a dictionary while checking for duplicate values. On my scene, I have a button that adds values to the dictionary and then displays them with a text box. There is a simple If-Else statement to check for duplicate entries, prior to entering the value.
On the first button press, the code works properly and adds the correct values. However, on subsequent button press it will only add the initial value, seeming to ignore my If-Else. I have tried different combinations in my If-Else, as well as coding for If not, with no success.
After running my code using debug.log, it is apparent that values are still being added to the dictionary.
I would love some insight into this issue. I know this is a simple bit of code, so I am really struggling to figure out the problem.
Thank you!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class GlobalFoodItems : MonoBehaviour {
public class Food
{
public string Name { get; set; }
public int CO2 { get; set; }
public int grams { get; set; }
}
public GameObject Item;
public void ItemDict()
{
Item.GetComponent<Text> ().text = "";
string b;
Dictionary<string, Food> fooditems = new Dictionary<string,Food> ();
if (fooditems.ContainsKey ("Bread"))
{
fooditems.Add ("Asparagus", new Food {Name = "Asparagus", CO2 = 21, grams = 100});
b = "Asparagus";
}
else
{
fooditems.Add ("Bread", new Food {Name = "Bread", CO2 = 20, grams = 100});
b = "Bread";
}
var d = fooditems [b];
int production;
production = d.grams * d.CO2;
Item.GetComponent<Text> ().text = "Food Item: " + d.Name + ". It produces " + production;
}
}
,Hello!
Im hoping this is a simple problem on my end. Essentially what I am doing is trying to add records to a database with a simple if-else code attached to stop duplicate key entries. The value is added via a button in my scene, and the subsequent value entered is displayed in a text box.
The issue I am running into is that regardless of how I set up my if else statement, it appears that either the statement isn't working, or the code is not starting over from the beginning (don't think so, but I am really racking my brain here.) What happens is that when I test the scene, if I click the button once I get the initial value added to the dictionary, and the resultant text displays properly. However, if I click the button again, it will add the initial value again and seem to ignore my If-Else statement. I have tried different variations of the If-Else, such as If(!Keyvalue.Containskey....) and trying different combinations with no success.
I used debug.log to confirm that it is adding a particular value, so I am a little stumped. I know this is a simple code, but I am going crazy trying to understand where the error is.
Thank you!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class GlobalFoodItems : MonoBehaviour {
public class Food
{
public string Name { get; set; }
public int CO2 { get; set; }
public int grams { get; set; }
}
public GameObject Item;
public void ItemDict()
{
Item.GetComponent<Text> ().text = "";
string b;
Dictionary<string, Food> fooditems = new Dictionary<string,Food> ();
if (fooditems.ContainsKey ("Bread"))
{
fooditems.Add ("Asparagus", new Food {Name = "Asparagus", CO2 = 21, grams = 100});
b = "Asparagus";
}
else
{
fooditems.Add ("Bread", new Food {Name = "Bread", CO2 = 20, grams = 100});
b = "Bread";
}
var d = fooditems [b];
int production;
production = d.grams * d.CO2;
Item.GetComponent<Text> ().text = "Food Item: " + d.Name + ". It produces " + production;
}
}
Your answer

Follow this Question
Related Questions
WHY !! CountDown not Working inside the IF Conndition !!! 2 Answers
oculus go movement 0 Answers
how to link URL into multiple buttons in a condition "if" 1 Answer
Change TextMeshPro text frequently after time 0 Answers
Material instance unwanted 1 Answer