I'm trying to keep track of some variables and keep my code efficient. I know the Unity API contains hashtables but it doesn't explain how to use them and/or it just straight up doesn't work. I've also tried the Microsoft C# version of dictionaries but that doesn't seem to work either. Here is the code I want to work:
public Hashtable OrbitSpeeds = new Hashtable();
//This doesn't work//
OrbitSpeeds.Add("Moon", 1.0f);
OrbitSpeeds.Add("Sattelite", -2.5f);
OrbitSpeeds.Add("Cloud", -3.0f);
float newSpeed = OrbitSpeeds["Cloud"];
OrbitSpeeds["Moon"] = 2.0f;
Dictionary <string, float> OrbitSpeeds = new Dictionary<string, float>();
//This also doesn't work//
OrbitSpeeds.Add("Moon", 1.0f);
OrbitSpeeds.Add("Sattelite", -2.5f);
OrbitSpeeds.Add("Cloud", -3.0f);
float newSpeed = OrbitSpeeds["Cloud"];
OrbitSpeeds["Moon"] = 2.0f;
It turns out the way to initialize and add things is to do it as so:
public class TAGS : MonoBehaviour
{
public static Dictionary<string, float> OrbitSpeeds= new Dictionary<string, float>
{
{"Moon", 1.0f},
{"Sattelite", -2.5f},
{"Cloud", -3.0f}
};
}
And this works exactly as intended
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.