Serialized field "Score" not updating in the inspector

I have an issue where the serialized field ‘Current Score’ doesn’t update when i test-play the game. I’ve included a Debug.log(currentScore); to see if it’s adding the score and it does just fine. here is the code for my gamestatus.cs:

public class GameStatus : MonoBehaviour {

    // Configuration parameters
    [Range(.01f, 10f)] [SerializeField] float gameSpeed;
    [SerializeField] int pointPerBlock = 10;

    // State variables
    [SerializeField] int currentScore = 0;

    
    // Update
    void Update () {
        Time.timeScale = gameSpeed;
	}    

    public void AddToScore()
    {
        currentScore += pointPerBlock;
        Debug.Log(currentScore);
    }
}

and here is the code for my brick.cs:

public class Brick : MonoBehaviour {

    public Sprite[] hitSprites;
    private int timesHit;

    void Start () {
        timesHit = 0;
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Ball")
        {            
            timesHit++;
            FindObjectOfType<GameStatus>().AddToScore();
            HandleHits();
        }        
    }

    void HandleHits()
    {
        int maxHits = hitSprites.Length;
        if (timesHit >= maxHits)
        {          
            Destroy(gameObject);            
        }
        else
        {
            LoadSprites();
        }
    }

    void LoadSprites()
    {
        int spriteIndex = timesHit;
        GetComponent<SpriteRenderer>().sprite = hitSprites[spriteIndex];
    }

}

I’ve ran through this 3 times. each time erasing the changes and starting this section over to see if maybe I had missed something. Still the results are the same. Any thoughts?

Hi Wayne,

Nothing is jumping out at me from looking at your code. Could you perhaps zip up your project files and share them so I could take a look?

The forum will allow uploads of up to 10MB, if your project files (zipped) are larger than that you would need to use a service such as Google Drive or Dropbox, and then share the URL.


See also;

https://1drv.ms/u/s!Ahvlun2GOnILlSCqzZ__WcIUMRy7

just a note. I’ve postponed creating a start screen, or a win screen. I wanted to get the main mechanics of operating the paddle.

paddle moves with mouse.
middle mouse button releases the ball.
up, down, left, and right arrow keys will turn the paddle the appropriate direction. although I have not attached a Lose collider. so for testing purposes, it’s not necessary.

Downloading and opening now… :slight_smile:


Updated Sat Sep 29 2018 23:51

Hi @Wayne_Dunn,

Ok, this is a nice easy one.

So, how can it be that the Debug.Log statement is being called and displaying messages to the console, but the variable we look at on the GameStatus GameObject doesn’t appear to be changing. The script is clearly ok, as it is updating the score and outputting the values to the console. So what else could be the problem.

Let’s take a look at the code used to call the AddToScore method;

private void OnCollisionEnter2D(Collision2D collision)
{
	if (collision.gameObject.tag == "Ball")
	{            
		timesHit++;
		FindObjectOfType<GameStatus>().AddToScore();
		HandleHits();
	}        
}

The FindObjectOfType method will return the first GameObject it finds of that type. Sounds ok doesn’t it. But what about if you have more than one object of that type, and the one you are looking at in the scene isn’t the one that it is returning? :slight_smile:

The problem you are experiencing is because you have the GameStatus.cs script attached to multiple GameObjects.

You are looking in the Inspector at the GameStatus GameObject, but it’s actually being updated on the PaddleShip GameObject, where you have a dupicate instance of this script component.

An easy way to locate the duplicates is to find the script within the Assets folder, and then right-click and select Find References In Scene;

This will then filter the Hierarchy and show you the GameObjects that have that script attached as a component, or reference it through parenting;

image

As you can see, there are three references to this script. Be aware that whilst Normal Ball is displayed, it is only displayed as a reference because it is actually a child GameObject of your PaddleShip GameObject, it doesn’t actually have this script attached as a component itself.

To resolve the problem, remove the duplicate GameStatus.cs script component from PaddleShip , remember, it’s a prefab, so if you remove it from the prefab it will update the instance in the scene.

Run the game.


(for increased clarity, maximise the video)

Hope this helps :slight_smile:

thank you, I will test it out tonight and let you know.!

1 Like

You’re welcome :slight_smile:

It works beautifully. Thank you Rob!, I will definitely remember this.

1 Like

Your are very welcome Wayne, happy to help :slight_smile:

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

Privacy & Terms