Score is not reading 0 if the player dies at start

This is my Score script

public class score1 : MonoBehaviour
{
    public static score1 instance;
    private Text scoreText;
    public int scr;
    void Start()
    {
        scr = 0;
    }
    // Start is called before the first frame update
    void Awake()
    {
        scoreText = GameObject.Find("scores").GetComponent<Text>();
        MakeInstance();
    }
    // Update is called once per frame
    void MakeInstance()
    {
        if (instance == null)
            instance = this;
    }
    public void IncrementScore()
    {
        scr++;
        scoreText.text = "Score:" + scr;
    }
    public int GetScore()
    {
        return this.scr;
    }
}

And this is the code which calls the increment if this is needed

if (transform.localScale.x <= 0.4f && score1.instance != null)
        {
            score1.instance.IncrementScore();
            PlayerPrefs.SetInt("Score", score1.instance.GetScore());
        }
if (transform.localScale.x <= 0.05f)
        {
            Destroy(gameObject);
        }

and if game is over at 0 its reading the previous score instead of 0

probably because IncrementScore( ) is never called as per the condition your provided. Put the function IncrementScore() in Start( ) so that it is at least called once.

1 Like

i tried it in start method but thn it started with 1 and kept updating instead of 0
.
.
.
.
solved it using playerpref at the start and nested if else i guess i learned something new