Reputation: 2724
Another follow on question from my last one.
Ive set up my dictionary and I'm passing in the key value and a distance value from another device. I'm wanting my program to update some local variables based on the distance values but only those that are associated with the key number in the dictionary.
All of this code looks like this:
Dictionary<string, string> myDictonary = new Dictionary<string, string>();
string Value1 = "";
string Value2 = "";
string Value3 = "";
string Value4 = "";
string Value5 = "";
string Value6 = "";
void Start()
{
myDictonary.Add("11111111", Value1);
myDictonary.Add("22222222", Value2);
myDictonary.Add("33333333", Value3);
myDictonary.Add("44444444", Value4);
myDictonary.Add("55555555", Value5);
myDictonary.Add("66666666", Value6);
}
private void AppendString(string message)
{
testMessage = message;
string[] messages = message.Split(',');
foreach(string w in messages)
{
if(!message.StartsWith(" "))
outputContent.text += w + "\n";
}
messageCount = "RSSI number " + messages[0];
uuidString = "UUID number " + messages[1];
if(myDictonary.ContainsKey(messages[1]))
{
myDictonary[messages[1]] = messageCount;
}
}
In the same application I'm also out putting all of my Values on the screen so that I can see them coming through correctly. This part of the program looks like this:
void OnGUI()
{
GUI.Label(new Rect(100, Screen.height/2 + 100, 150, 100), "value 1 " + Value1);
GUI.Label(new Rect(100, Screen.height/2 + 120, 200, 100), "value 2 " + Value2);
GUI.Label(new Rect(100, Screen.height/2 + 140, 250, 100), "value 3 " + Value3);
GUI.Label(new Rect(100, Screen.height/2 + 160, 300, 100), "value 4 " + Value4);
GUI.Label(new Rect(100, Screen.height/2 + 180, 350, 100), "value 5 " + Value5);
GUI.Label(new Rect(100, Screen.height/2 + 200, 400, 100), "value 6 " + Value6);
}
Before I implemented the fix from my last question, whilst it may have been wrong, the strings did update and were displayed on my screen. Since I placed the new code in, nothing happens anymore.
I've tried changing the following method:
if(myDictonary.ContainsKey(messages[1]))
{
myDictonary[messages[1]] = messageCount;
}
So that instead of the first message in the array getting check and passed over to every possible combination between the two. And still the same issue are in the system.
Can anyone see anything?
Upvotes: 0
Views: 131
Reputation: 16257
Maybe I'm misreading your code, but setting myDictonary[messages[1]] = messageCount will have no effect on the values of the string variables Value1 through Value5. Setting the dictionary value replaces the object associated with the key. It does not change the internal value of the object variable originally associated with the key.
Try the following:
void OnGUI()
{
GUI.Label(new Rect(100, Screen.height/2 + 100, 150, 100), "value 1 " + myDictonary["11111111"]);
GUI.Label(new Rect(100, Screen.height/2 + 120, 200, 100), "value 2 " + myDictonary["22222222"]);
GUI.Label(new Rect(100, Screen.height/2 + 140, 250, 100), "value 3 " + myDictonary["33333333"]);
GUI.Label(new Rect(100, Screen.height/2 + 160, 300, 100), "value 4 " + myDictonary["44444444"]);
GUI.Label(new Rect(100, Screen.height/2 + 180, 350, 100), "value 5 " + myDictonary["55555555"]);
GUI.Label(new Rect(100, Screen.height/2 + 200, 400, 100), "value 6 " + myDictonary["66666666"]);
}
Or even better:
void OnGUI()
{
int top = 100;
int left = 150;
foreach(var keyValue in myDictonary) {
GUI.Label(new Rect(100, Screen.height/2 + top, left, 100), keyValue.Key + " " + keyValue.Value);
top += 20;
left += 50;
}
}
Upvotes: 1