Why wont this reset the value of the number to 0?

In a 1v1 score system I made, the value of both scores should go down to 0 if one side reaches 5, but it only works with one side. I’m fairly new to c#, so I dont know if this is a good way to do it. would appreciate any help!

This Code Doesnt Work:

 {
        public GameObject Player2Score;
        public static int ScoreValue = 0;

        
        Text score;
    
        void Start()
        {
            score = GetComponent<Text>();
            GameObject.Find("Player1Score");
        }
    
        void Update()
        {
            score.text = "" + ScoreValue;
            if (ScoreValue >= 5)
            {
                ScoreValue = 0;
                SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
            }
            if (ScoreScript2.ScoreValue2 == 5)
            {
                ScoreValue = 0;
            }
        }
    }

This Code does

{   
    public GameObject Player1Score;
    public static int ScoreValue2 = 0;
    
        Text score;

    void Start()
    {
        score = GetComponent<Text>();
        GameObject.Find("Player2Score");
    }

    void Update()
    {
        score.text = "" + ScoreValue2;
        if (ScoreValue2 >= 5)
        { 
            ScoreValue2 = 0;
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 2);
        }

        if (ScoreScript.ScoreValue >= 5)
        {
            ScoreValue2 = 0;
        }      
    }
}

Point Adder:

{
    private void OnTriggerEnter2D(Collider2D collision)
    {

        ScoreScript2.ScoreValue2 += 1;
        SceneManager.LoadScene("MainGame");
    }
}

Other Point Adder:

{
    private void OnTriggerEnter2D(Collider2D collision)
    {

        ScoreScript.ScoreValue += 1;
        SceneManager.LoadScene("MainGame");

    }
}

Not sure where I went wrong, because I think that both codes are the exact same apart from the class and Integer names.

Not sure where I went wrong, because I think that both codes are the exact same apart from the class and Integer names.

Well, yes they are both very similar. But have you ever thought about when and in which order the code is executed? Code does not somehow “happen” all at once. You have two objects each with a different script attached. Each frame Unity will call the Update method of all scripts on all active objects in the scene. However they are executed one by one.

Now just play through any scenario step by step. You can even figure out which of the two scripts run first and which will run second. I’m assuming by “this works” you mean when “ScoreScript2.ScoreValue2” reaches 5 both score values will be reset? If that’s the case we know in which order your scripts are executed. Your first script runs first and your second script runs after the first. The reason is quite simple when ScoreValue2 turned 5 and the first script runs it’s update method it sees that ScoreValue2 is 5 and will reset ScoreValue to 0. After that the Update of your second script runs which will also detect that ScoreValue2 is 5 and will set ScoreValue2 to 0.

Now just imagine the opposite case and assume ScoreValue turned 5. Again your first script’s Update runs first. It will see ScoreValue is 5 and will reset ScoreValue to 0. After that your second script’s Update runs. However ScoreValue is now 0 since the first script just set it to 0, so the condition for the if statement is not met and nothing will happen.

Apart from the ordering issue, you also call SceneManager.LoadScene in the first script. This will load a new scene so the Update method of the second script most likely will not run at all when the condition in the first script is met.

You should learn how to debug your code. Try placing some Debug.Log statements in your code so you can see which parts of your code executed and in which order.