ContainsKey returns a bool [True,False] to indicate whether the key exists in the Dictionary.
You’re getting the error because myString is a string and you’re trying to compare it against the bool type returned by ContainsKey.
Does not work. You are telling it to compare a string to a boolean.
if(myDictionary.ContainsKey(myOne))
You only need this, but all it does is confirm that the list actually has a string that == myOne.
Further, you don’t seem to actually know how to code at all. You’re placing code out of scope and have very poor syntax comprehension. I’d suggest starting here before you get overwhelmed.
I’m still not sure what you’re trying to achieve, but this code will add the key and value to the Dictionary.
Then check that that key is in the Dictionary, and compare the value corresponding to that key with your string value.
Again, I’m not sure what you trying to do, because of course the key and value will be in the Dictionary, you just added them.
As @LaneFox mentioned, all code that is not a declaration must be inside a method.
You had your code to add entries to the Dictionary outside of the Start method, this isn’t allowed.
public string myString = "Two";
public string myOne = "One";
public string myTwo = "Two";
Dictionary<string, string> myDictionary = new Dictionary<string, string> ();
void Start()
{
myDictionary.Add(myOne, myTwo);
if(myDictionary.ContainsKey(myOne) == true)
{
if (myDictionary[myOne] == myTwo)
{
Debug.Log("OK");
}
else
{
Debug.Log("myDictionary contains " + myDictionary[myOne]);
}
}
else
{
Debug.Log(myOne + " does not exist in myDictionary");
}
}
Assets/Scripts/Store/StoreScript.cs(125,21): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
If you look at the matching line in the code I posted (line 9). You’ll see that you need to use rounded brackets on a method call, not square brackets.
Using Google would be better for you, because you won’t have to wait so long for an answer. Just copying your question into google gave me answer to your question.