- Home /
Question by
helmidh · Nov 20, 2020 at 10:58 AM ·
sliderpublic variableautomatically
Why is my public variable slider resetting itself on start?,Why are the public slider variables resetting themselves on play?
For my UI health bars, I set the slider variables to public so I could simply drag what I needed.
However, when I hit play, the variables are reset, so the health bars don't change.
Can someone please tell me why this is happening and how to resolve it?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIManager : MonoBehaviour
{
public Slider playerHealth;
public Slider boss1Health;
public Slider boss2Health;
private bossScript bossScript1;
void Awake()
{
playerHealth = GetComponent<Slider>();
boss1Health = GetComponent<Slider>();
boss2Health = GetComponent<Slider>();
}
public void SetPlayerHealth(int health)
{
playerHealth.value = health;
}
public void SetBossHealth(int health)
{
boss1Health.value = health;
boss2Health.value = health;
}
public void Update()
{
if (boss1Health.value == 0)
{
boss1Health.enabled = false;
boss2Health.enabled = true;
//boss1Health.gameObject.SetActive(false);
//boss2Health.gameObject.SetActive(true);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
GUI Slider sets a value but the value appears to be reset by the end of OnGUI() 1 Answer
How I can change this command to my Keyboard? 1 Answer
Rotation with a slider 2 Answers
Horizontal Slider 1 Answer
How to Center Horizontal Thumb Slider? 0 Answers