Question about Heatlh Bar system

Hello Goodnight. I have a question with running this code: I am following the following guidelines:

Which makes me generate three files, with the following codes:

Health.cs

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

public class Health : MonoBehaviour
{

   [SerializeField]
   private Stat heatlh;

    [SerializeField]
    private float fillAmount;

    [SerializeField]
    private Image content;

    public float MaxValue { get; set; }

    public float Value
    {
        set {
            fillAmount = Map(value, 0, MaxValue, 0, 1);
        }   
    }   

    void Update() {

        HandleBar();
    }

    private void HandleBar() {

        if (fillAmount != content.fillAmount)
        {
            content.fillAmount = fillAmount;
        }
    }

    private float Map( float value, float inMin, float inMax, float outMin, float outMax)
    {
        return (value - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
    }
}

Stat.cs

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

[SerializeField]
public class Stat
{

    [SerializeField]
    private Health bar;

    [SerializeField]
    private float maxVal;

    [SerializeField]
    private float currentVal;

    public float CurrentVal
    {
        get {

            return currentVal;
        }

        set {

            this.currentVal = value;
            bar.Value = currentVal;
        }
    }

    public float MaxVal
    {
        get {
            return maxVal;
        }

        set {

            this.maxVal = value;
            bar.MaxValue = maxVal;
        }
    }
}

CharacterMovement.cs

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

public class CharacterMovement : MonoBehaviour
{
    private Rigidbody2D rbd2;
    public Animator animator;

    [SerializeField]
    private Stat health;
   
    /*
     *  Movimientos básicos.
     */

    public float Speed = 10.0f;
    public float jumpForce = 300.0f;
    public bool facingRight = true;
       
    /*
     *  Verificamos si el personaje está en contacto con el suelo.
     */

    public Transform groundCheck;
    public float groundRadius = 0.2f;
    public bool grounded = false;
    public LayerMask whatIsGround;

    // Start is called before the first frame update
    void Start()
    {
        rbd2 = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void FixedUpdate()
    {     

        float move = Input.GetAxis("Horizontal");

        if (move < 0) rbd2.velocity = new Vector3(move * Speed, rbd2.velocity.y);
        if (move > 0) rbd2.velocity = new Vector3(move * Speed, rbd2.velocity.y);

        if (move < 0 && facingRight) Flip();
        if (move > 0 && !facingRight) Flip();

        animator.SetFloat("speedChar", Mathf.Abs(move));

        bool jump = Input.GetButtonDown("Jump");

        grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);

        if (jump && grounded) rbd2.AddForce(Vector3.up * jumpForce);

    }
    void Flip()
    {
        facingRight = !facingRight;
        transform.Rotate(Vector3.up * 180);
    }  
}

Well … everything works fine apparently. When I run the game everything seems to work but in the inspector the variables of bar, maxVal and CurrentVal (Video min 14:33)

In my game, I can´t see this variables: game hosted at ImgBB — ImgBB

Will it be the tutorial version or is something missing?

Regards!

The Canvas configuration is the same as in the tutorial. I have to put it together again because I undid it, since I tried other guides.

This part is wrong:

[SerializeField]
public class Stat

Switch it to:

[System.Serializable]
public class Stat

SerializeField will only work on fields inside of classes.

1 Like

Ready! Thanks!

That was. It’s already working. I never noticed the difference, I’m sorry. :frowning: