Search Unity

gameobject.setactive not working for me

Discussion in 'Editor & General Support' started by qbasty, Feb 16, 2020.

  1. qbasty

    qbasty

    Joined:
    Feb 16, 2020
    Posts:
    2
    Hello. I'm new to unity and I'm trying to hide health bar when pause menu is open and show it again when pause menu is closed. Health bar is hiding when i open pause menu but it isn't showing up when i close pause menu. Thanks.


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    public class HealthBar : MonoBehaviour
    {
    public Slider slider;
    public Gradient gradient;
    public Image fill;
    public GameObject pause;
    public void SetMaxHealth(int health)
    {
    slider.maxValue = health;
    slider.value = health;
    fill.color = gradient.Evaluate(1f);
    }
    public void SetHealth(int health)
    {
    slider.value = health;
    fill.color = gradient.Evaluate(slider.normalizedValue);
    }
    public void Update()
    {
    if (pausemenu2.GameIsPaused)
    {
    gameObject.SetActive(false);
    }
    else
    {
    gameObject.SetActive(true);
    }
    }
    }
     
  2. mcmount

    mcmount

    Joined:
    Nov 15, 2015
    Posts:
    83
    It might be because you disable the object having this script.... When it's disabled, also this script is disabled.
     
  3. qbasty

    qbasty

    Joined:
    Feb 16, 2020
    Posts:
    2
    Is there anything I can do or is there other way to hide this object
     
  4. mcmount

    mcmount

    Joined:
    Nov 15, 2015
    Posts:
    83
    GetComponent(MeshRenderer).enabled = false;
    (If you are trying to hide an object without disabling it completely).