Getting elements from an array in a dictionary

0
I have a dictionary with a string for a key, and a string array for a value. I’d like to be able to call for a value of the array in a single step, but I’m running into problems.

This works:

var MyDictionary : Dictionary.<String,String[]>;
var tempVar : String[];

function Start(){

    MyDictionary = ("Key Name", ["Value 1", "Value 2", "Value 3"]);
    tempVar = MyDictionary["Key Name"];
    Debug.Log(tempVar[0]);
}

But if I try to save a step and jump direction into the dictionary, like below, I get an error saying ‘The given key was not present in the dictionary.’ Am I stuck using an intermediate variable, or is there a way around this?

var MyDictionary : Dictionary.<String,String[]>;

function Start(){

    MyDictionary = ("Key Name", ["Value 1", "Value 2", "Value 3"]);
    Debug.Log(MyDictionary["Key Name"][0]);
}

You’re doing more than just jumping a step, you are using a different approach. Dictionaries are made to give the value of the keyword, so you don’t need to add the [0] part of the statement:

Debug.Log(MyDictionary[“Key Name”]);

Should give you the value stored. That’s what you did in the first part of your example when you made a variable equal to tempVar = MyDictionary[“Key Name”];