Search Unity

Slider Active

Discussion in 'Scripting' started by Leandre5, Jul 14, 2019.

  1. Leandre5

    Leandre5

    Joined:
    Nov 1, 2017
    Posts:
    42
    Hello to all of you,

    It may be a silly subject, but I've been searching a lot without finding a real answer.

    I would like to disable a slider but it doesn't work.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Enemy : MonoBehaviour {
    7.  
    8. public Slider HealthBar;
    9.  
    10.  
    11.  
    12.  
    13.     void Start ()
    14.     {
    15.         HealthBar.enabled = false;
    16.     }
     
    DubiDuboni likes this.
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    use
    Code (CSharp):
    1. HealthBar.intearactable = false;
    instead
     
  3. DubiDuboni

    DubiDuboni

    Joined:
    Feb 5, 2019
    Posts:
    131
    Uset SetActive :

    Code (csharp):
    1.  
    2. HealthBar.gameObject.SetActive(false);
    3.  
     
    g3n3 likes this.
  4. DubiDuboni

    DubiDuboni

    Joined:
    Feb 5, 2019
    Posts:
    131
    Or if you want to disable the slider script and not the gameobject :

    Code (csharp):
    1.  
    2. slider.GetComponent<Slider>().enabled = false;
    3.  
     
  5. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Just to be clear because all these are subtly different:

    • SetActive(false) operates on the game object and will make it and all it's children disappear. scrips on these objects will no longer receive Update() invocations
    • somecomponent.enabled = false will cause Unity to no longer call Update() on that script if the parent object is active; all other methods remain invokable
    • someUIcomponent.isinteractable = false ONLY works on UI elements that descend from UI.selectable (e.g. a slider) and will cause it to be unresponsive to clicks, and be drawn accordingly.

    -ch
     
    Gcbs and DubiDuboni like this.
  6. DubiDuboni

    DubiDuboni

    Joined:
    Feb 5, 2019
    Posts:
    131
    True.
     
  7. Leandre5

    Leandre5

    Joined:
    Nov 1, 2017
    Posts:
    42
    Thank you very much to all of you for your feedback