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 Hans Larsen · Sep 28, 2013 at 07:31 PM · c#

Losing references made in editor script

Hi

I am trying to learn editor scripting, and I am experiencing stuff that I find weird, which probably because I don't understand how Unity works.

I am trying to create a grid of GameObjects. I would like to store references to the created GameObjects in a Dictionary (or a two dimensional array), but it seems like I lose the references when going from editor mode to game mode/running the game.

I have tried to make a small code example to show my problem:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class GridHandler : MonoBehaviour {
 
     public GameObject gameObject;
     public List<GameObject> list = new List<GameObject>();
     public Dictionary<string, GameObject> dictionary = new Dictionary<string, GameObject>();
     
     void Start(){
         //Using Start() to test in "game mode"
         //I would like to use the references to the gameObjects that I generated in createGrid.
         Debug.Log("Start");
         Debug.Log(gameObject.transform.name);
         Debug.Log(list[0].transform.name);
         Debug.Log(dictionary["a"].transform.name); //This call gives error "KeyNotFoundException"
     }
      
     //This function is called from an editor script in "editor mode"
     public void createGrid(){
         //Clearing just for testing
         list.Clear();
         dictionary.Clear();
         
         gameObject = new GameObject();
         gameObject.transform.name = "1 - gameObject";
        
         list.Add(new GameObject());
         list[0].transform.name = "2 - list";
 
         dictionary.Add("a", new GameObject());
         dictionary["a"].transform.name = "3 - dictionary";
 
         Debug.Log(gameObject.transform.name);
         Debug.Log(list[0].transform.name);
         Debug.Log(dictionary["a"].transform.name);
     }
 }




In the Console i get:

(createGrid called)

1 - gameObject

2 - list

3 - dictionary

(running program)

1 - gameObject

2 - list

Error (KeyNotFound)

I could store the grid in a list and let each gameObject know its position, it just doesn't seem optimal, and I would like to understand why I can't use a Dictionary. It seems like I have the same problem when using dynamically generated two-dimensional arrays.

Please ask if I havn't explained my problem precisely.

Best wishes Hans Larsen

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

1 Reply

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

Answer by Jamora · Sep 28, 2013 at 08:01 PM

Dictionaries aren't serialized when changing play modes. There are ways around it, either transform your dictionary to two lists (which are serialized by Unity) and rebuilding the dictionary on entry to Game Mode (like explored in this question) or create a custom serializer that handles your data. The asset store has a few of those.

Comment
Add comment · Show 1 · 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 Hans Larsen · Sep 28, 2013 at 09:31 PM 0
Share

Ok, thank you. I can see that I have some reading to do about serializing.

For anyone with similar problems, this seems like a good read: http://docs.unity3d.com/Documentation/ScriptReference/SerializeField.html

I will return if there is something I don't understand.

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

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

If there is only 1 instance of a class, should it be Static? - C# 2 Answers

Call a Void on Collision 2 Answers

AI Patrolling 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