Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

I need little help to modify slider script

Discussion in 'Scripting' started by Uccione, Oct 18, 2018.

  1. Uccione

    Uccione

    Joined:
    Oct 17, 2018
    Posts:
    4
    I created one slider and transformed to HP bar, but i need to modify my script.
    I would like to get it off when I press a button (UI) and once he arrives at 0 scene change.
    Someone could help me?
    Thank you in advance


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class hp : MonoBehaviour {
    7.    
    8.     public Slider BarraHp;
    9.     public int Hp = 100;
    10.    
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.        
    15.     }
    16.    
    17.     void OnTriggerEnter (Collider other) {
    18.         if (other.gameObject.tag == "sotto5HP") {
    19.             Hp = Hp - 5;
    20.         }
    21.     }
    22.    
    23.     // Update is called once per frame
    24.     public void Update () {
    25.        
    26.         BarraHp.value = Hp;
    27.        
    28.        
    29.     }
    30. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    I'll be honest, not sure what you are asking. What do you mean by get it off and 0 scene change?

    Also, just as a note, no reason to update BarraHp.value in Update. Just update the value after you subtract the Hp in the OnTriggerEnter. That way you change the value once instead of trying to change it every frame, even when you haven't taken any damage.
     
    Uccione likes this.
  3. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    My best guess:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. // Include to use scene manager functions (for loading scenes).
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class HP : MonoBehaviour
    8. {
    9.     public Slider barraHp;
    10.     public int hp = 100;
    11.  
    12.     private void OnTriggerEnter(Collider other)
    13.     {
    14.         // Use CompareTag for comparing tags. It's better for performance.
    15.         if (other.gameObject.CompareTag("sotto5HP"))
    16.         {
    17.             hp -= 5;
    18.             barraHp.value = hp;
    19.  
    20.             // Load new scene if hp reaches 0.
    21.             if (hp <= 0)
    22.             {
    23.                 // Put the index of the scene you want to load here. The index is shown in File -> Build Settings.
    24.                 // You can also put the scene name here as: SceneManager.LoadScene("GameOver") for example.
    25.                 SceneManager.LoadScene(0);
    26.             }
    27.         }
    28.     }
    29.  
    30.     // Hook this up to the button you want to press. In the inspector, click on your button and go to where it
    31.     // shows On Click (list is empty). Click the +, add the object with this HP script on it, and select this
    32.     // function. It will then be called when you click the button.
    33.     public void OnButtonPress()
    34.     {
    35.         // I am not sure how you want the slider removed. This disables the entire game object.
    36.         // You can also destroy it: Destroy(barraHp.gameObject) or disable the slider itself
    37.         // barraHp.enabled = false.
    38.         barraHp.gameObject.SetActive(false);
    39.     }
    40. }
    I also included what Brathnann suggested as it is more efficient.
     
    Uccione likes this.
  4. Uccione

    Uccione

    Joined:
    Oct 17, 2018
    Posts:
    4

    the hp range is 0-100 and I wanted when I press the UI button to go down by 5, when it comes to 0 hp it would change scene. Sorry for my bad english, i m italian.
     
  5. Uccione

    Uccione

    Joined:
    Oct 17, 2018
    Posts:
    4
    OH Grazie Mille Jason:)
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    To note, this is using OnTriggerEnter, if you want it to happen off a button press, I would suggest you create a different method that you can link to the button in the inspector.

    If you also want it to trigger off the player hitting something, you could move the code to another method, but leave the compareTag check and just have it call the other method if it's true.
     
    Uccione likes this.
  7. Uccione

    Uccione

    Joined:
    Oct 17, 2018
    Posts:
    4
    Eh I thought about it too, onclick instead the trigger?
    As a function it should only have that pressing the ui button loses life, and that at the end of life it changes scene.
    Brathnann, what method would you recommend to me? Thank you
     
    Last edited: Oct 19, 2018