Changing Variables based off of Number of GameObjects

Hello! To summarize the problem I am having, I need to find a way to change a variable based off of the number of a specific Game Object in a Level Automatically.

The actual explanation is I have Buttons that are pressed by the Player in order to progress through levels. I decided to make this progression happen in a Button Script and a Level Manager Script. However, this variable does not change based off of the number of Buttons in a Level; so if there are two buttons in a Level, then only one button is required to be pressed, rather than two. That is where I need help.

How can I change a variable based off of the Number of a specific Game Object Automatically?

Note: The Code is pasted below. Thanks for any help anyone can give.

Level Manager Script:

public class LevelManager : MonoBehaviour
{
    public int NumberOfButtonPress = 0;

    // Need a way to change THIS Variable based off of the number of buttons in a Level Automatically
    public int RequiredButtonPress = 1;

    public static LevelManager instance;

    // Start is called before the first frame update
    void Start()
    {
        if (instance == null)
            instance = this;
        else
        {

            Destroy(gameObject);
            return;
        }

        DontDestroyOnLoad(gameObject);

      
      
    }


    void Update()
    {
      
          
             
                if (NumberOfButtonPress == RequiredButtonPress || NumberOfButtonPress > RequiredButtonPress)
                {
                    NumberOfButtonPress = 0;
               
                    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);

                }
          
    }

  
}

Button Script:

public class ButtonScript : MonoBehaviour
{
    public LevelManager NumberOfButtonPress;
    public LevelManager RequiredButtonPress;
    private bool recentbuttonPress;
    ...

    // Start is called before the first frame update
    private void Start()
    {
        // Upon the start of the Level, this bool is set to false (as the player has not pressed the button(s))
        recentbuttonPress = false;

      
      
      
    }

  
    void OnTriggerEnter2D(Collider2D other)
    {
        // If the Player collides with the button, the button is checked off as a pressed button.
        if (other.gameObject.CompareTag("Player") && recentbuttonPress == false)
        {
            recentbuttonPress = true;
            StartCoroutine (Button());
          
        }

    }

    IEnumerator Button()
    {
      
        GameObject.Find("LevelManager").GetComponent<LevelManager>().NumberOfButtonPress += 1;
...
}

Steps to success:

  • Put some identifying marker on the objects you care about counting
    —> could be a tag?
    —> could be a custom empty MonoBehaviour?
    —> some other way of identifying them?

  • find all those objects and see how many there are by counting the size of the returned array.

FindGameObjectsWithTag() will find tagged GameObjects

FindObjectsOfType<T>() will find particular custom MonoBehaviours

So after writing some code, that solution works! Thanks for the help! :slight_smile: