Observable List

STEP 1: Use it just like a Normal List

public class GameObjectSpawner : MonoBehaviour
{
    public GameObjectVar prefab;
    public ObservableList<GameObject> spawnedObjects = new();
    public void Spawn()
    {
        GameObject spawned = Instantiate(prefab.Value, transform.position, transform.rotation);
        spawned.name = "G" + Random.Range(0, 1000000);
        
        // Add the spawned object to the Observable List
        spawnedObjects.Add(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