Cannot read dictionary properties from another script

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
```csharp
**public Dictionary<string, Dictionary<List, bool>> geometryDict =
new Dictionary<string, Dictionary<List, bool>>();

private void Update() {
if (geometryDict.ContainsKey(“cell-23”)) {
print(geometryDict[“cell-23”]);
print(“collision controller”);
}
}**
```

DataController.cs

private CollisionController collisionController;

    private void Awake() {
        collisionController = FindObjectOfType<CollisionController>();
    }

private void Update() {
    if (collisionController.geometryDict.ContainsKey("cell-23")) {
        print(collisionController.geometryDict["cell-23"]);
        print("data controller");
    }

probably your reference is not working correctly.

     collisionController = FindObjectOfType<CollisionController>();

try dragging it into the inspector manually and see if it works

1 Like

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. :confused:

Is there any chance that OntriggerEnter causes this? Because I initiated a list on Start and the other script reads it properly.

Just try those changes in order to debug your issue: In your CollisionController script do this:

    public Dictionary<string, Dictionary<List<string>, bool>> geometryDict =
            new Dictionary<string, Dictionary<List<string>, bool>>();
    
    private void Update()
    {
        if (geometryDict.ContainsKey("cell-23"))
        {
            Debug.Log("collision controller", gameObject);
        }
    }

Do a similar thing in your Data controller:

    private CollisionController collisionController;
    
    private void Awake()
    {
        collisionController = FindObjectOfType<CollisionController>(); 
        Debug.Log("DataController::this", gameObject);
        Debug.Log("DataController::collisionController", collisionController);
    }

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 :slight_smile:

1 Like

Thanks, I will try it! :slight_smile:

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

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. )

1 Like

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. :confused:

whats your code for filling the dictionaries?

it must be hairy since you are filling dictionaries of dictionaries, maybe theres a problem there

1 Like

It could be hairy; I’m a novice in Unity scripting. :slight_smile:

        if (gameObject.CompareTag("cell") && triggered.CompareTag("Flammable")) {
            if (!flammableCellsList.Contains(triggered.name)) {
                flammableCellsList.Add(triggered.name);
                var tempList = flammableCellsList;
             
                cellDictionary.Add(tempList, true);
             
                geometryDict.Add(gameObject.name, cellDictionary2);
            }
        }

(thnx for helping me btw) :slight_smile:

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! :slight_smile: