Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Cannot read dictionary properties from another script

Discussion in 'Scripting' started by v_chs, Oct 13, 2020.

  1. v_chs

    v_chs

    Joined:
    Dec 11, 2019
    Posts:
    64
    I have two scripts and I want to pass a dictionary from the first to the second one:
    • CollisionController (Fill the dictionary)
    • DataController (Read the dictionary)
    In the first, I fill a dictionary in OnTriggerMethod, and on Update, I validate that it is filled properly; the prints are shown properly. On the second (DataController) I create an instance of the script but on Update, I don't see any values on the debugger. It's like it is empty. Any ideas why is this happening?

    CollisionController.cs

    Code (CSharp):
    1. public Dictionary<string, Dictionary<List<string>, bool>> geometryDict =
    2.         new Dictionary<string, Dictionary<List<string>, bool>>();
    3.  
    4. private void Update() {
    5.         if (geometryDict.ContainsKey("cell-23")) {
    6.             print(geometryDict["cell-23"]);
    7.             print("collision controller");
    8.         }
    9.     }


    DataController.cs
    Code (CSharp):
    1. private CollisionController collisionController;
    2.  
    3.     private void Awake() {
    4.         collisionController = FindObjectOfType<CollisionController>();
    5.     }
    6.  
    7. private void Update() {
    8.     if (collisionController.geometryDict.ContainsKey("cell-23")) {
    9.         print(collisionController.geometryDict["cell-23"]);
    10.         print("data controller");
    11.     }
     
  2. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    probably your reference is not working correctly.

    Code (CSharp):
    1.      collisionController = FindObjectOfType<CollisionController>();
    2.    
    try dragging it into the inspector manually and see if it works
     
    v_chs likes this.
  3. v_chs

    v_chs

    Joined:
    Dec 11, 2019
    Posts:
    64
    This thing with the references drives me crazy in Unity.

    I tried it on Inspector and it didn't work. The dict.Count is constantly 0. :/
     
  4. v_chs

    v_chs

    Joined:
    Dec 11, 2019
    Posts:
    64
    Is there any chance that OntriggerEnter causes this? Because I initiated a list on Start and the other script reads it properly.
     
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,528
    Just try those changes in order to debug your issue: In your CollisionController script do this:

    Code (CSharp):
    1.     public Dictionary<string, Dictionary<List<string>, bool>> geometryDict =
    2.             new Dictionary<string, Dictionary<List<string>, bool>>();
    3.    
    4.     private void Update()
    5.     {
    6.         if (geometryDict.ContainsKey("cell-23"))
    7.         {
    8.             Debug.Log("collision controller", gameObject);
    9.         }
    10.     }
    11.  
    Do a similar thing in your Data controller:
    Code (CSharp):
    1.     private CollisionController collisionController;
    2.    
    3.     private void Awake()
    4.     {
    5.         collisionController = FindObjectOfType<CollisionController>();
    6.         Debug.Log("DataController::this", gameObject);
    7.         Debug.Log("DataController::collisionController", collisionController);
    8.     }
    9.    
    10.  
    Now let this code run. You may want to press "pause" in order to examine your game at runtime without having anything changing. When you now click on one of our debug messages in the console, Unity will highlight the context object that we passed along to the Debug.Log call. This should help identifying the objects involved.

    That's not "a thing" specifically in Unity. OOP is pretty pointless if you don't know what objects you're working with :)
     
    v_chs likes this.
  6. v_chs

    v_chs

    Joined:
    Dec 11, 2019
    Posts:
    64
    Thanks, I will try it! :)

    I have a web development background -especially in Angular and Node.js- and I have some difficulties adapting to this way of development.
     
  7. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    how many collision controllers do you have on your scene?

    you are probably dragging one that does not have the dictionary filled

    ( also as a bonus try making everything public instead of private to make sure some access limitations are not blocking you, after you have it fixed you can switch the things you need back to private. )
     
    v_chs likes this.
  8. v_chs

    v_chs

    Joined:
    Dec 11, 2019
    Posts:
    64
    A
    After some tests, I understand that the dictionaries that are getting filled in OntriggerEnter of the CollisionControler cannot be used from the DataController script. I filled a list in Start method and the DataController reads it properly. When I filled it in OnTriggerEnter I could not access it. :/
     
  9. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    whats your code for filling the dictionaries?

    it must be hairy since you are filling dictionaries of dictionaries, maybe theres a problem there
     
    v_chs likes this.
  10. v_chs

    v_chs

    Joined:
    Dec 11, 2019
    Posts:
    64
    It could be hairy; I'm a novice in Unity scripting. :)

    Code (CSharp):
    1.         if (gameObject.CompareTag("cell") && triggered.CompareTag("Flammable")) {
    2.             if (!flammableCellsList.Contains(triggered.name)) {
    3.                 flammableCellsList.Add(triggered.name);
    4.                 var tempList = flammableCellsList;
    5.              
    6.                 cellDictionary.Add(tempList, true);
    7.              
    8.                 geometryDict.Add(gameObject.name, cellDictionary2);
    9.             }
    10.         }
    (thnx for helping me btw) :)
     
  11. v_chs

    v_chs

    Joined:
    Dec 11, 2019
    Posts:
    64
    I figured out another way and it worked. I attached the script to an empty object and I use this only for filling the dictionaries. The other script was attached to many objects so maybe it was causing issues.

    Thank you anyway! :)