Score is not updating properly

I have a problem with my score where I go from 750 to 300 to 450 to 600 750 900 and then 7050 …

my score display is:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ScoreDisplay : MonoBehaviour
{

    Text scoreText;
    GameSession gameSession;

    // Use this for initialization
    void Start()
    {
        scoreText = GetComponent<Text>();
        gameSession = FindObjectOfType<GameSession>();
    }

    // Update is called once per frame
    void Update()
    {
        scoreText.text = gameSession.GetScore().ToString();
    }
}

my game session is:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameSession : MonoBehaviour
{

    int score = 0;

    private void Awake()
    {
        SetUpSingleton();
    }

    private void SetUpSingleton()
    {
        int numberGameSessions = FindObjectsOfType<GameSession>().Length;
        if (numberGameSessions > 1)
        {
            Destroy(gameObject);
        }
        else
        {
            DontDestroyOnLoad(gameObject);
        }
    }

    public int GetScore()
    {
        return score;
    }

    public void AddToScore(int scoreValue)
    {
        score += scoreValue;
    }

    public void ResetGame()
    {
        Destroy(gameObject);
    }

}

I’ve called the score:

 FindObjectOfType<GameSession>().AddToScore(scoreValue);

And … the score addes itself up, but not counting from 0 to 150’s

This is where I set the score to 150:

I can’t send I vidoe of the problem because it’s a mov. … However, I could send screenshots of the video

The first time I score it shows this:

2nd time:


3rd time:

4th time

5th time:

6th time:

7th time:
43%20PM

Really weird … and I haven’t got the slightest clue to why it’s happening. All my scripts are right. I attached them in the right places, so why is the score going up like crazy. I have nothing random in the scripts I have. So why is it like this why do I start at 750, and then go like normal untill I get passed 900 where I have 7050???

Hi Ahron,

If you have a problem and need to share a lot of information, it is better to create your own thread. I did that for you this time.

A quick fix for the SetUpSingleton method in your GameSession class:

if (numberGameSessions > 1)
{
        gameObject.SetActive(false); // <---- this
        Destroy(gameObject);
}

Test your game. If that does not fix it, keep the fix. It’ll be added by Rick at a later juncture.

Add Debug.Logs to your code to see how often your AddToScore method gets called and what value(s) get passed on to it.

2 Likes

I don’t see anything happening here, same thing.

  private void SetUpSingleton()
    {
        int numberGameSessions = FindObjectsOfType<GameSession>().Length;
        if (numberGameSessions > 1)
        {
            gameObject.SetActive(false);
            Destroy(gameObject);
        }
        else
        {
            DontDestroyOnLoad(gameObject);
        }
    }

I would be curious to see the hierarchy while the game is running too. That would tell us if you have multiple game managers.

Add a debug.log to your game for your score variable, for when you add to the score. You can see what’s happening more clearly when you add debug.logs to your code.

21%20AM

Please do not use an alpha or beta version of Unity for your actual projects. They are full of bugs. Downgrade to a stable version with an “f” in its version number.

I have done so, but now I get a bunch of new errors. I suspect this has to do with me downgrading the unity program?

Here is a screenshot of the errors, and version number.

Unfortunately, Unity always installs a bunch of packages when you create a new game. Go to Window > Package Manager and uninstall all packages that you currently do not need. For example, the error message says something about a Rider plugin. Uninstall that. If it’s not in the Package Manager, go to your Assets folder and look for it there.

If you get more error messages that seem to point to a package, read it and try to look for the concerning package.

I don’t see such an option.

Could you share a screenshot of your package manager here?

You may want to cache that into a variable…

private GameSession gameSession; 

void Start()
{
   gameSession = FindObjectOfType<GameSession>();
}

the call…
gameSession.AddToScore(scoreValue);

Then you will find out if your gameSession variable ever goes to NULL. You will get an exception if it fails. Since you would only cache it at the Start() function, this way if that fixes it then you likely are having more GameSession Instances created then expected. By caching you will always only call the one that was cached. The way you have the AddToScore written it could be finding a new GameSession every call.

Your expectation with a singleton would say you should only ever get one. However, you can code it to be sure you only use one reference, by using the method I show. Not to mention FindObjectOfType<> is a very expensive call. It is best to call it as little as possible and at the beginning of the Scene.

Looks like the font you are using have very similar 7’s and 1’s. In the first picture you posted of your score the 7 has another bar (that you’d usually find in a 7), whereas the “5th time”, and the one with 7050 (I assume it is supposed to be 1050, an 150 point increment from 900) don’t. Have you tried changing fonts to see if it might be related to that?

:smiley: I think your very right … time to change fonts XD

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

Privacy & Terms