Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Need help health bar slider

Discussion in 'Scripting' started by Pixitales, Feb 26, 2019.

  1. Pixitales

    Pixitales

    Joined:
    Oct 24, 2018
    Posts:
    227
    I am trying to make a music/rhythm game. I have been following "gamesplusjames" tutorials on Youtube, but he didn't upload any new ones in months. Wonderful tutorials.

    I am using a slider UI for my health bar and a text UI to keep track of my health points. The health tracker text works but the health bar slider doesn't move. It stays at the value of 3 as my starting health even if you change the value to max 10. I already linked my slider to my GameManager's Inspector.

    Ignore the score and multiplier system... those works perfectly at the moment

    Not sure if this mean anything when moving the slider manually...
    SendMessage cannot be called during Awake, CheckConsistency, or OnValidate
    UnityEngine.GUIUtility:processEvent(Int32, IntPtr)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class GameManager : MonoBehaviour {
    7.  
    8.     public AudioSource theMusic;
    9.  
    10.     public bool startPlaying;
    11.  
    12.     public BeatScroller theBS;
    13.  
    14.     public static GameManager instance;
    15.  
    16.     public Slider healthSlider; //Health Bar Slider
    17.  
    18.     public int healthPoints;
    19.     public int loseHealth = 1;
    20.  
    21.     public int currentScore;
    22.     public int scorePerNote = 100;
    23.  
    24.     public int currentMultiplier;
    25.     public int multiplierTracker;
    26.     public int[] multiplierThresholds;
    27.  
    28.     public Text healthText; //Health Text UI
    29.     public Text scoreText; //Score Text UI
    30.     public Text multiText; //Multiplier Text UI
    31.  
    32.  
    33.     // Use this for initialization
    34.     void Start () {
    35.         instance = this;
    36.  
    37.         healthText.text = "Health: 3";
    38.         scoreText.text = "Score: 0";
    39.         currentMultiplier = 1;
    40.     }
    41.  
    42.     // Update is called once per frame
    43.     void Update () {
    44.         if(!startPlaying)
    45.         {
    46.             if(Input.anyKeyDown)
    47.             {
    48.                 startPlaying = true;
    49.                 theBS.hasStarted = true;
    50.  
    51.                 theMusic.Play();
    52.                 healthSlider.value = healthPoints; //Health Bar = Current health points
    53.             }
    54.         }
    55.     }
    56.  
    57.     public void NoteHit() //When you hit a music note
    58.     {
    59.         Debug.Log("Hit On Time");
    60.  
    61.         if (currentMultiplier - 1 < multiplierThresholds.Length)
    62.         {
    63.             multiplierTracker++;
    64.  
    65.             if (multiplierThresholds[currentMultiplier - 1] <= multiplierTracker)
    66.             {
    67.                 multiplierTracker = 0;
    68.                 currentMultiplier++;
    69.             }
    70.         }
    71.  
    72.         multiText.text = "Multiplier: x" + currentMultiplier;
    73.  
    74.         currentScore += scorePerNote *currentMultiplier;
    75.         scoreText.text = "Score: " + currentScore;
    76.     }
    77.  
    78.     public void NoteMissed() //When you miss a music note
    79.     {
    80.         Debug.Log("Missed Note");
    81.  
    82.         currentMultiplier = 1;
    83.         multiplierTracker = 0;
    84.  
    85.         multiText.text = "Multiplier: x" + currentMultiplier;
    86.  
    87.         healthPoints -= loseHealth; //Subtract health point
    88.         healthText.text = "Health: " + healthPoints;
    89.     }
    90. }
     
    Last edited: Feb 26, 2019
  2. Pixitales

    Pixitales

    Joined:
    Oct 24, 2018
    Posts:
    227
    nevermind I fixed it... i put the health bar = current health points in the wrong place

    Here is the updated code. Mostly codes from the tutorial anyways but with a few stuff added into it. I still got a lot of stuff to work on.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class GameManager : MonoBehaviour {
    7.  
    8.     public AudioSource theMusic;
    9.  
    10.     public bool startPlaying;
    11.  
    12.     public BeatScroller theBS;
    13.  
    14.     public static GameManager instance;
    15.  
    16.     public Slider healthSlider; //Health Bar Slider
    17.  
    18.     public int healthPoints;
    19.     public int loseHealth = 1;
    20.  
    21.     public int currentScore;
    22.     public int scorePerNote = 100;
    23.  
    24.     public int currentMultiplier;
    25.     public int multiplierTracker;
    26.     public int[] multiplierThresholds;
    27.  
    28.     public Text healthText; //Health Text UI
    29.     public Text scoreText; //Score Text UI
    30.     public Text multiText; //Multiplier Text UI
    31.  
    32.  
    33.     // Use this for initialization
    34.     void Start () {
    35.         instance = this;
    36.  
    37.         healthText.text = "Health: 3";
    38.         scoreText.text = "Score: 0";
    39.         currentMultiplier = 1;
    40.     }
    41.  
    42.     // Update is called once per frame
    43.     void Update () {
    44.         if(!startPlaying)
    45.         {
    46.             if(Input.anyKeyDown)
    47.             {
    48.                 startPlaying = true;
    49.                 theBS.hasStarted = true;
    50.  
    51.                 theMusic.Play();
    52.             }
    53.         }
    54.     }
    55.  
    56.     public void NoteHit() //When you hit a music note
    57.     {
    58.         Debug.Log("Hit On Time");
    59.  
    60.         if (currentMultiplier - 1 < multiplierThresholds.Length)
    61.         {
    62.             multiplierTracker++;
    63.  
    64.             if (multiplierThresholds[currentMultiplier - 1] <= multiplierTracker)
    65.             {
    66.                 multiplierTracker = 0;
    67.                 currentMultiplier++;
    68.             }
    69.         }
    70.  
    71.         multiText.text = "Multiplier: x" + currentMultiplier;
    72.  
    73.         currentScore += scorePerNote *currentMultiplier;
    74.         scoreText.text = "Score: " + currentScore;
    75.     }
    76.  
    77.     public void NoteMissed() //When you miss a music note
    78.     {
    79.         Debug.Log("Missed Note");
    80.  
    81.         healthSlider.value = healthPoints; //Health Bar = Current health points
    82.  
    83.         currentMultiplier = 1;
    84.         multiplierTracker = 0;
    85.  
    86.         multiText.text = "Multiplier: x" + currentMultiplier;
    87.  
    88.         healthPoints -= loseHealth; //Subtract health point
    89.         healthText.text = "Health: " + healthPoints;
    90.     }
    91. }
    92.  
     
    Last edited: Feb 26, 2019