Search Unity

Question about Heatlh Bar system

Discussion in 'Scripting' started by Middrel, Jan 23, 2020.

  1. Middrel

    Middrel

    Joined:
    Oct 19, 2016
    Posts:
    72
    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
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Health : MonoBehaviour
    7. {
    8.  
    9.    [SerializeField]
    10.    private Stat heatlh;
    11.  
    12.     [SerializeField]
    13.     private float fillAmount;
    14.  
    15.     [SerializeField]
    16.     private Image content;
    17.  
    18.     public float MaxValue { get; set; }
    19.  
    20.     public float Value
    21.     {
    22.         set {
    23.             fillAmount = Map(value, 0, MaxValue, 0, 1);
    24.         }  
    25.     }  
    26.  
    27.     void Update() {
    28.  
    29.         HandleBar();
    30.     }
    31.  
    32.     private void HandleBar() {
    33.  
    34.         if (fillAmount != content.fillAmount)
    35.         {
    36.             content.fillAmount = fillAmount;
    37.         }
    38.     }
    39.  
    40.     private float Map( float value, float inMin, float inMax, float outMin, float outMax)
    41.     {
    42.         return (value - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
    43.     }
    44. }
    Stat.cs
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [SerializeField]
    6. public class Stat
    7. {
    8.  
    9.     [SerializeField]
    10.     private Health bar;
    11.  
    12.     [SerializeField]
    13.     private float maxVal;
    14.  
    15.     [SerializeField]
    16.     private float currentVal;
    17.  
    18.     public float CurrentVal
    19.     {
    20.         get {
    21.  
    22.             return currentVal;
    23.         }
    24.  
    25.         set {
    26.  
    27.             this.currentVal = value;
    28.             bar.Value = currentVal;
    29.         }
    30.     }
    31.  
    32.     public float MaxVal
    33.     {
    34.         get {
    35.             return maxVal;
    36.         }
    37.  
    38.         set {
    39.  
    40.             this.maxVal = value;
    41.             bar.MaxValue = maxVal;
    42.         }
    43.     }
    44. }
    45.  
    CharacterMovement.cs
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class CharacterMovement : MonoBehaviour
    7. {
    8.     private Rigidbody2D rbd2;
    9.     public Animator animator;
    10.  
    11.     [SerializeField]
    12.     private Stat health;
    13.    
    14.     /*
    15.      *  Movimientos básicos.
    16.      */
    17.  
    18.     public float Speed = 10.0f;
    19.     public float jumpForce = 300.0f;
    20.     public bool facingRight = true;
    21.        
    22.     /*
    23.      *  Verificamos si el personaje está en contacto con el suelo.
    24.      */
    25.  
    26.     public Transform groundCheck;
    27.     public float groundRadius = 0.2f;
    28.     public bool grounded = false;
    29.     public LayerMask whatIsGround;
    30.  
    31.     // Start is called before the first frame update
    32.     void Start()
    33.     {
    34.         rbd2 = GetComponent<Rigidbody2D>();
    35.         animator = GetComponent<Animator>();
    36.     }
    37.  
    38.     // Update is called once per frame
    39.     void FixedUpdate()
    40.     {    
    41.  
    42.         float move = Input.GetAxis("Horizontal");
    43.  
    44.         if (move < 0) rbd2.velocity = new Vector3(move * Speed, rbd2.velocity.y);
    45.         if (move > 0) rbd2.velocity = new Vector3(move * Speed, rbd2.velocity.y);
    46.  
    47.         if (move < 0 && facingRight) Flip();
    48.         if (move > 0 && !facingRight) Flip();
    49.  
    50.         animator.SetFloat("speedChar", Mathf.Abs(move));
    51.  
    52.         bool jump = Input.GetButtonDown("Jump");
    53.  
    54.         grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
    55.  
    56.         if (jump && grounded) rbd2.AddForce(Vector3.up * jumpForce);
    57.  
    58.     }
    59.     void Flip()
    60.     {
    61.         facingRight = !facingRight;
    62.         transform.Rotate(Vector3.up * 180);
    63.     }  
    64. }
    65.  
    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: https://ibb.co/s9ZpncJ

    Will it be the tutorial version or is something missing?

    Regards!
     
  2. Middrel

    Middrel

    Joined:
    Oct 19, 2016
    Posts:
    72
    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.
     
  3. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    This part is wrong:
    Code (CSharp):
    1. [SerializeField]
    2. public class Stat
    Switch it to:
    Code (CSharp):
    1. [System.Serializable]
    2. public class Stat
    SerializeField will only work on fields inside of classes.
     
    Middrel likes this.
  4. Middrel

    Middrel

    Joined:
    Oct 19, 2016
    Posts:
    72
    Ready! Thanks!

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