Observable Dictionary

STEP 1: Use it just like a Normal Dictionary

public class GameObjectSpawner_Dict : MonoBehaviour
{
    public GameObjectVar prefab;
    public ObservableDictionary<string, GameObject> spawnedObjects = new();
    public void Spawn()
    {
        GameObject spawned = Instantiate(prefab.Value, transform.position, transform.rotation);
        spawned.name = "G" + Random.Range(0, 1000000);
        // Add the object to the Observable Dictionary
        spawnedObjects.Add(spawned.name, spawned);
    }
}

STEP 2: Listen to the changes

void Awake(){
    spawnedObjects.OnChange += (op, index, oldVal, newVal) => {
        Debug.Log("Spawned object changed: " + op + " " + index + " " + oldVal + " " + newVal);
    };
}

Last updated