r/UnityHelp • • May 22 '22

Invalid token '=' in class, struct, or interface member declaration

So I am trying to make a engine simulation game and I am getting an error when using dictionaries

Code:

var dictionaryDouble = new Dictionary<String, double>();

dictionaryDouble["Throttle"] = 0.0f;

and

var dictionaryBool = new Dictionary.< String, bool>();

dictionaryBool["engineOn"] = false;

can someone help me with this?

2 Upvotes

11 comments sorted by

2

u/SamElTerrible May 22 '22

If you want to add a new entry to your dictionary (for example Dictionary<string, bool>) you need to do:

MyDictionary.Add("my string", true);

1

u/PenguinPolarStuff May 22 '22

Gives me 5x as many errors

1

u/Sharkytrs May 22 '22 edited May 22 '22

dictionarys are just lists with 2D array (they can hold two bits of info, a key (your string), and a value (the double)

so you use them the same as lists and add an entry to it i.e

dictionaryDouble.Add("Throttle",0.0f);

dictionaryBool.Add("engineOn",false);

etc

then refer back to them by key or index

by key returns the value

so

dictionaryDouble("Throttle");

would return 0.0f

where as by index it almost works like Linq if you've ever used Linq in .net

dictionaryDouble.Keys.ElementAt(0)

would return Throttle and

dictionaryDouble.Values.ElementAt(0)

would return 0.0f

1

u/PenguinPolarStuff May 22 '22

Gives me 5x as many errors

1

u/Sharkytrs May 22 '22

well that's how dictionary's work.

https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2?view=net-6.0

https://learn.unity.com/tutorial/lists-and-dictionaries

you could always use enumerations if you aren't planning on changing said values.

1

u/PenguinPolarStuff May 22 '22

What do you mean "That's how dictionary's work.".

1

u/Sharkytrs May 22 '22

i outlined how they work.

if you've took on board what I've said and implemented it correctly then thats how they work so it will work.

you have stated, "that gives 5 x more errors"

thats pretty useless if you want help debugging. So I showed you the API for them in .net, and a unity learn article on how they apply to unity. So you can look it up and see where you went wrong.

1

u/PenguinPolarStuff May 22 '22

Errors:

Assets\Scripts\Engine.cs(9,25): error CS1519: Invalid token '(' in class, struct, or interface member declaration

Assets\Scripts\Engine.cs(9,26): error CS1031: Type expected

Assets\Scripts\Engine.cs(9,26): error CS8124: Tuple must contain at least two elements.

Assets\Scripts\Engine.cs(9,26): error CS1026: ) expected

Assets\Scripts\Engine.cs(9,26): error CS1519: Invalid token '"Throttle"' in class, struct, or interface member declaration

Assets\Scripts\Engine.cs(25,23): error CS1519: Invalid token '(' in class, struct, or interface member declaration

Assets\Scripts\Engine.cs(25,24): error CS1031: Type expected

Assets\Scripts\Engine.cs(25,24): error CS8124: Tuple must contain at least two elements.

Assets\Scripts\Engine.cs(25,24): error CS1026: ) expected

Assets\Scripts\Engine.cs(25,24): error CS1519: Invalid token '"engineOn"' in class, struct, or interface member declaration

Code: GitHub

1

u/Sharkytrs May 22 '22

dude what do you code in? half these issues will be highlighted with intellisense in visual studio if its setup right.

the additions to the dictionary on line 9 + 25 needs to be in start, you can't add things to a list outside of runtime, if its not in a function, its not in runtime. it sees them as attempting to make a member declaration, which is completely not what you are trying to do.

declaring the variable statically outside of runtime to make a member decleration is fine. i.e

private static Dictionary<string, double> dictionaryDouble = new Dictionary<string, double>{ {"Throttle",0.0f} };

a trick to the error codes are the the "Assets\Scripts\Engine.cs(9,25): " bit

the bit in brackets is the line number at fault.

2

u/PenguinPolarStuff May 22 '22

TY this worked!

1

u/Sharkytrs May 22 '22

Awesome!