Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Falkrons · May 04, 2015 at 01:31 PM · c#serializationdictionary

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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/

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Nested Dictionary data lost after playmode 0 Answers

From two lists into a dictionary... and back? 5 Answers

Serializing data structures the Unity way. Possible? 1 Answer

In this case the lists do not serialize. Why? 2 Answers

Multiple Cars not working 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges