Array of dictionaries: "Object reference not set to an instance of an object"
Hi, I'm trying to have an array of dictionaries (which has GameObject references as its keys and Vector3s as its values), where each index in the array refers to one "step" in time (each "step" refers to one move within the game), so that I can then use this to create a simple "undo" feature, where the player can press z to undo his previous action, which would mean the game would first get the dictionary at the previous array index, then iterate through this array, setting each GameObject to its position, which was recorded as its corresponding value in the dictionary.
Unity doesn't seem to be having a problem with how I am using the array of dictionaries, but rather with how I am referring to the objects in some way.
Here is the relevant code:
object[] all_objects;
int step;
Dictionary<GameObject, Vector3>[] positions_record;
[...]
void Record()
{
all_objects = FindObjectsOfType(typeof(GameObject));
Dictionary<GameObject, Vector3> record_dictionary = new Dictionary<GameObject, Vector3>();
for (int i = 0; i < all_objects.Length; i++)
{
GameObject obj = (GameObject) all_objects[i];
if (obj.activeInHierarchy && obj.tag != "ladder")
{
record_dictionary.Add(obj, obj.transform.position);
}
}
positions_record[step] = record_dictionary;
}
Record() gets called in Start() as well as after every move that the player makes. However, every time it is called, Unity throws the following error message:
NullReferenceException: Object reference not set to an instance of an object
(wrapper stelemref) object:stelemref (object,intptr,object)
Any help would be much appreciated.
Your answer
Follow this Question
Related Questions
Object reference not set to an instance of an object [HELP!] 0 Answers
[Begginer] [Solved] How to solve a Null Reference Exception 1 Answer
Keep Getting NullReferenceException: Object reference not set to an instance of an object 0 Answers
Random Chance NullReferenceException: Object reference not set to an instance of an object 2 Answers