Using delegates to keep track of colliders

I am trying to keep track of whether or not all the colliders in the scene have been deactivated using:

GetComponent().enabled=false;

When I click on the game objects using OnMouseDown, the collider is set to false so I can no longer click on the object.

I have heard you can use delegates, but they are confusing me not having used them before. I want to have the game end when all the colliders in the scene have been deactivated. Any ideas or help would be great.

Not sure how delegates would help you specifically in this case. Unless your ultimate goal is more complex or de-coupled logic.

I’d just have everything that’s supposed to be clicked on register itself when the level starts. Then unregister itself when it gets clicked on. If the number of registered things is 0 then the game is over

static List<GameObject> colliderThings = new List<GameObject>();

void Start()
{
    colliderThings.Add(gameObject);
}

void OnMouseDown()
{
    GetComponent<Collider>().enabled = false;
    colliderThings.Remove(gameObject);
    if (colliderThings.Count == 0)
    {
        GameOver();
    }
}
1 Like

This is good and I think this will work. I haven’t used a list like this, and when I declare the List it says that the ‘list’ does not exist in the current context. Do I need to declare the list in the same way I would a string?

using System.Collections.Generic;

using keyword

List(T)

1 Like

Thanks!