I can't see the update of Current Hit Points on the Inspector

Hi there!

The script is working fine, however I can’t see the current value of the serialized variable currentHitPoints in real time. So, it shows the initialization value of 0, and it never updates itself. What am I missing?
Btw, I’m using Unity 2022.3.37f1.git.3410856 from Linux.

Hi donlaiq, welcome to the community.

Please post the script and a screen shot of the inspector.

Oh, that was really quick :slight_smile: !

That’s the code:

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

public class EnemyHealth : MonoBehaviour
{
    [SerializeField] int maxHitPoints = 5;
    [SerializeField] int currentHitPoints = 0;

    void Start()
    {
        currentHitPoints = maxHitPoints;
    }

    void OnParticleCollision(GameObject other)
    {
        ProcessHit();
    }

    void ProcessHit()
    {
        currentHitPoints--;
        if(currentHitPoints <= 0)
            Destroy(gameObject);
    }
}

Once I play run, and according to the lesson video, I should see Current Hit Points going from 5 to 0. Instead, it always shows 0.

Captura desde 2024-07-29 19-08-06

Again, the code does what it’s supposed to do, but it doesn’t show this “debugging” clue.

I don’t see anything in this that is different than in the video. This looks like it should work perfectly. Do you have the Enemy Health Script attached to the enemy? (I know I’m grasping at straws!) Maybe put a debug.log in start to print the current hit points after they are set?

Yes, I’ve attached the Enemy Health Script to the enemy (otherwise I wouldn’t be able to see the previous attached image).
And yes, sure, I could use some debug.log lines to print some message to the console, but that’s not my intention. I was just curious about this Unity UI behavior in running mode, because maybe there was some option to activate/deactivate it, or maybe the Linux version works slightly different, I don’t know.
Anyway, it is not really a hindrance to keep working. So, thank you for your help, edc237! :slight_smile:

Hi donlaiq,

Did you select the correct enemy? A common mistake is to select the prefab. Prefabs are not in the scene, hence they don’t take part in your game, and their currentHitPoints value doesn’t change.

Thank you, Nina!

You are right!
The problem was about my endless stupidity. Yes, indeed, I was selecting the prefab and not the current instance.
Thanks! :+1:

No worries. The reason why I suspected that you made this mistake was because I made (and sometimes still make) it, too. Many game objects look basically “the same” in the Inspector. That’s why I usually double check if I selected the correct game object if nothing seems to happen. :slight_smile:

And you are still working for Gamedev! When you need someone with expertise like yours, feel free to contact me, and then we can make the same mistakes together. :grin:

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

Privacy & Terms