Declaring reference type variable inside function or inside class

which one is better:

    public Text scoreBoard;
    public int score;
    public string scoreText;

    void Update()
    {
        scoreText = "Score: " + score.ToString();
        scoreBoard.text = scoreText;
    }

or

    public Text scoreBoard;
    public int score;

    void Update()
    {
        string scoreText = "Score: " + score.ToString();
        scoreBoard.text = scoreText;
    }

or are both same?

I like the second one better.

Reason: I don’t like to duplicate data (no good reason exists to store the score as BOTH a number AND a string, keep just one). This keeps the save file, and memory footprint, smaller.

Exception: if the computation was more extensive that a simple number to string conversion- some process that required significant time to compute: then it might be worth storing both versions of the data.

Worth noting: by NOT assigning the member the “public” keyword (in example 1), you will be preventing that data from being saved to disk. (Though this would NOT reduce the runtime memory footprint.)

I would just do it all on one line to simplify things since scoreText is unnecessary.

scoreBoard.text = "Score: " + score.ToString();