Why not just use static variables?

public class Block : MonoBehaviour
{
    [SerializeField] private AudioClip breakSound;
    private static int breakableBlocks;
    private static int blocksDestroyed;

    private SceneLoader sceneLoader;

    void Start()
    {
        breakableBlocks++;

        sceneLoader = FindObjectOfType<SceneLoader>();
    }

    void OnCollisionEnter2D(Collision2D collision)
    {
        AudioSource.PlayClipAtPoint(breakSound, Camera.main.transform.position);
        Destroy(gameObject);

        ++blocksDestroyed;

        if (blocksDestroyed == breakableBlocks)
        {
            sceneLoader.LoadNextScene();
        }
    }
}

Hi Chris,

Welcome to our community! :slight_smile:

static variables might appear convenient but they are also dangerous because all Block objects could manipulate the value of that variable. Debugging might become challenging.

Also see here. https://stackoverflow.com/questions/7026507/why-are-static-variables-considered-evil/7026563#7026563

Did this help?


See also:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms