- Home /
does this key not found error have to do with serialization?
First I am rather new and am working with C#. I have been researching this issue for a while now and have not been able to resolve myself so I bring this to the community. My brain is on the verge of exploding with the overload of information.
I am trying to assign the instanced versions of a Prefab to a dictionary for quick access later. The problem is it seems that "later" is not soon enough for me. I have two scripts (SpawnController and TankController) on different gameObjects. The gameObject with the SpawnController on it has only one instance while the gameObjects with the TankController are many. The SpawnController has a dictionary that has information assigned during the Awake function. I then attempt to access this information on the newely Instantiated gameObject.tankController's Start function, which is where the errors seems to occur.
KeyNotFoundException: The given key was not present in the dictionary. System.Collections.Generic.Dictionary`2[System.Int32,UnityEngine.GameObject].get_Item (Int32 key) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150) TankController.Start () (at Assets/scripts/TankController.cs:38)
Here are the scripts: using UnityEngine; using System.Collections; using System.Collections.Generic;
public class SpawnController : MonoBehaviour {
public Transform[] tankSpawn;
public GameObject tank;
public int numberOfPlayers;
public Dictionary<int, GameObject> tanks = new Dictionary<int, GameObject>();
void Awake () {
GameObject tankInstance;
for (int i = 0; i < numberOfPlayers; i++) {
tankInstance = Instantiate(tank, tankSpawn[i].position, tankSpawn[i].rotation)as GameObject;
tanks.Add (i, tankInstance);
}
}
}
TankController script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TankController : MonoBehaviour {
public GameObject manager; // gameObject reference for the SpawnController
SpawnController spawncontroller;
void Start () {
spawncontroller = manager.GetComponent<SpawnController> ();
if (spawncontroller.tanks[0] == this) {
//do something
} else if (spawncontroller.tanks[1] == this) {
//do something else
} else if (spawncontroller.tanks[2] == this) {
//do something else
} else if (spawncontroller.tanks[3] == this) {
//do something else
}
I have verified the variables are all correct and accessible; however, the dictionary returns empty at every point until the Start function of the SpawnController script. I checked within the for loop, the Awake function of both scripts, I tried my own custom function that was called inside the for loop of SpawnController and every other place I thought useful...all returned 0 on Debug.Log (tanks.Count); until, as I mentioned the Start function of the original script where it returned correctly. I started looking into serialization and was wondering if that has anything to do with this; however before I spent many more hours trying to understand serialization I figured I would ask the community? Thank you for your help in advance!
Answer by Falkrons · May 05, 2015 at 03:09 AM
I actually figured it out over on the forums side. I was not providing the TankController a reference to the instanced version of the Manager object, so it was seeing the prefab version which was always empty.
THE FIX: manager = GameObject.FindGameObjectWithTag ("GameManager");
http://forum.unity3d.com/threads/does-this-key-not-found-error-have-to-do-with-serialization.323350/
Answer by siaran · May 04, 2015 at 01:48 PM
On Phone sorry but sounds like you have 0 players and dictionary doesnt get filled in the loop.