Points and Score not showing in Inspector

As per the lesson, I’ve serialized pointsPerBlockDestroyed and currentScore in my GameStatus script, and that script has been added to my Game Status prefab. I can see those two fields in the Inspector, but they constantly stay at 0 (I’ve tried restarting Unity / Visual Studio, as well as rebooting). I just can’t figure out why they aren’t updating. Below are relevant screenshots and scripts.

Game Status Inspector
game_status_inspector

Console output
console_output

GameStatus.cs

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

public class GameStatus : MonoBehaviour
{
    // configuration parameters
    [Range(0.1f, 10f)] [SerializeField] float gameSpeed = 1f;
    [SerializeField] int pointsPerBlockDestroyed = 83;

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

    // Update is called once per frame
    void Update()
    {
        Time.timeScale = gameSpeed;
    }

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

Block.cs

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

public class Block : MonoBehaviour
{
    [SerializeField] AudioClip breakSound;

    // Cached reference
    Level level;

    private void Start()
    {
        level = FindObjectOfType<Level>();
        level.CountBreakableBlocks();
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        DestroyBlock();
    }

    private void DestroyBlock()
    {
        FindObjectOfType<GameStatus>().AddToScore();
        AudioSource.PlayClipAtPoint(breakSound, Camera.main.transform.position);
        Destroy(gameObject);
        level.BlockDestroyed();  
    }
}

Hi @mikee,

Welcome to our community! :slight_smile:

When assigning a script to an Inspector, the Inspector uses the default values at the top of your code to initialise the field. Then it takes control over the fields. If you change the default value in your code, the already existing component in your Inspector will not get affected by it.

Also bear in mind that Unity runs code of components active in the Hierarchy. If you manipulate values of prefabs during runtime, the game objects in the Hierarchy will not get affected by it.

I do not know what exactly you did or what your goal was but maybe this information will help you find the problem in your project. :slight_smile:


See also:

Hi Nina,

Thank you! That all makes sense, but this is the bit that doesn’t seem to be happening: “Then it takes control over the fields”. You can see this happen from 7:53 in Rick’s video - he clicks on Game Status in the Inspector and we can see that Points Per Block has been initialized as 83, then when he plays, the Score updates as the blocks are destroyed.

In mine, both Points and Score stay as 0, both before and during play. I haven’t changed anything during runtime.

Thanks,

Mike

Please log the value of pointsPerBlockDestroyed into the console. Maybe the value is 0 as seen in your screenshot.

0 + 0 = 0.

Here’s the console output:
image

And here’s the updated GameStatus code for reference (I just added the Start method and put the debug line in there):

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

public class GameStatus : MonoBehaviour
{
    // configuration parameters
    [Range(0.1f, 10f)] [SerializeField] float gameSpeed = 1f;
    [SerializeField] int pointsPerBlockDestroyed = 83;

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

    private void Start()
    {
        Debug.Log(pointsPerBlockDestroyed);
    }

    // Update is called once per frame
    void Update()
    {
        Time.timeScale = gameSpeed;
    }

    public void AddToScore()
    {
        currentScore += pointsPerBlockDestroyed;
    }
}

Great. Your console shows the problem: pointsPerBlockDestroyed is obviously 0. :slight_smile:

For this reason, currentScore += pointsPerBlockDestroyed; increases currentScore by 0 each time this line gets executed. x + 0 = x.

And where does the value of pointsPerBlockDestroyed come from? Here is a cutout of your first screenshot in this thread (plus a red arrow pointing to the 0):

image

If you are wondering why the value is not 83, please refer to my previous answer.

You could either type 83 into the field, or you could click the three dots in the component box and reset the component. Do not do that during runtime. Otherwise, the changes will get lost.

Ah that’s fixed it! I reset the component in both scenes and it picked up the 83 and started tallying the score correctly. Thank you so much!

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

Privacy & Terms