Search Unity

UI Slider is not reducing its value

Discussion in 'Scripting' started by mdddarwin, Feb 15, 2019.

  1. mdddarwin

    mdddarwin

    Joined:
    Sep 7, 2018
    Posts:
    33
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6.  
    7. public class GameController : MonoBehaviour
    8. {
    9.  
    10.     public static GameController GC;
    11.  
    12.     private UserInput player { get { return FindObjectOfType<UserInput>(); } set { player = value; } }
    13.  
    14.     private PlayerUI playerUI { get { return FindObjectOfType<PlayerUI>(); } set { playerUI = value; } }
    15.  
    16.     private WeaponHandler wp { get { return player.GetComponent<WeaponHandler>(); } set { wp = value; } }
    17.  
    18.     private CharacterStats stats { get { return player.GetComponent<CharacterStats>(); } set { stats = value; } }
    19.  
    20.     void Awake()
    21.     {
    22.         if (GC == null)
    23.         {
    24.             GC = this;
    25.         }
    26.         else
    27.         {
    28.             if (GC != this)
    29.             {
    30.                 Destroy(gameObject);
    31.             }
    32.         }
    33.     }
    34.  
    35.     void Update()
    36.     {
    37.         UpdateUI();
    38.     }
    39.     void UpdateUI()
    40.     {
    41.         if(player)
    42.         {
    43.             if (playerUI)
    44.             {
    45.                 if (wp)
    46.                 {
    47.                     if (wp.currentWeapon == null)
    48.                     {
    49.                         playerUI.ammoText.text = "Unarmed";
    50.                     }
    51.                     else
    52.                     {
    53.                         playerUI.ammoText.text = wp.currentWeapon.ammo.clipAmmo + "/" + wp.currentWeapon.ammo.maxClipAmmo + "|" + wp.currentWeapon.ammo.carryingAmmo;
    54.                     }
    55.                 }
    56.  
    57.                 if(playerUI.HealthSlider && playerUI.HealthText)
    58.                 {
    59.                     playerUI.HealthText.text = Mathf.Round(playerUI.HealthSlider.value).ToString();
    60.                     playerUI.HealthText.text = stats.Startinghealth + "/" + stats.MaxHealth;
    61.                     playerUI.HealthSlider.value = stats.Startinghealth;
    62.                 }
    63.             }
    64.         }
    65.     }
    66. }
     
  2. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    A slider's range by default is between 0 - 1.

    Assuming that your MaxHealth value is greater than 1, either set the slider's Max Value to the player's max health or divide the player's current health by max health in your code.
     
  3. mdddarwin

    mdddarwin

    Joined:
    Sep 7, 2018
    Posts:
    33
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class CharacterStats : MonoBehaviour
    6. {
    7.  
    8.     private CharacterController characterController { get { return GetComponent<CharacterController>(); } set { characterController = value; } }
    9.     private RagdollManager ragdollManager { get { return GetComponentInChildren<RagdollManager>(); } set { ragdollManager = value; } }
    10.     private PlayerUI UI { get { return GetComponent<PlayerUI>(); } set { UI = value; } }
    11.  
    12.     private CharacterStats stats;
    13.  
    14.     [Range(0, 100)]
    15.     public int Startinghealth = 100;
    16.  
    17.     public int MaxHealth = 100;
    18.  
    19.     public int faction;
    20.  
    21.     public MonoBehaviour[] scriptsToDisable;
    22.  
    23.     bool damage;
    24.  
    25.     void Update()
    26.     {
    27.         Startinghealth = Mathf.Clamp(Startinghealth, 0, 100);
    28.         UpdateHealth();
    29.     }
    30.  
    31.     public void Damage(int amount)
    32.     {
    33.         Startinghealth -= amount;
    34.  
    35.         if (Startinghealth <= 0)
    36.         {
    37.             Die();
    38.         }
    39.     }
    40.     void Die()
    41.     {
    42.         characterController.enabled = false;
    43.  
    44.         if (scriptsToDisable.Length == 0)
    45.         {
    46.             return;
    47.         }
    48.         foreach (MonoBehaviour script in scriptsToDisable)
    49.             script.enabled = false;
    50.  
    51.         if (ragdollManager != null)
    52.             ragdollManager.RagDoll();
    53.  
    54.         Destroy(gameObject, Random.Range(10, 20));
    55.     }
    56.  
    57.     public void HealthPack()
    58.     {
    59.         Startinghealth = MaxHealth;
    60.     }
    61.  
    62.     public void UpdateHealth()
    63.     {
    64.         UI.HealthSlider.value = Startinghealth;
    65.     }
    66. }
    this is my character stats
     
  4. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    What is your Slider's "Max Value" set to?
     
  5. mdddarwin

    mdddarwin

    Joined:
    Sep 7, 2018
    Posts:
    33
    it is Set to 1
     
  6. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Set it to 100 (MaxHealth).