Slider Component Problem (Health Bar)
Hey guys!
I’m creating simple health bar using slider, here is script: public class PlayerController : MonoBehaviour { public float moveSpeed;
public float jumpForce;
public float playerHealth;
public float maxPlayerHealth;
private float moveVelocity;
public float jumpPlus = 15f;
private bool isGrounded;
private bool speedSelected;
private bool jumpSelected;
private Rigidbody2D myBody;
public GameObject shopPanel;
public GameObject info;
public Slider healthBar;
// Use this for initialization
void Start () {
myBody = GetComponent<Rigidbody2D> ();
healthBar = GetComponent<Slider> ();
playerHealth = maxPlayerHealth;
healthBar.value = playerHealth;
}
// Update is called once per frame
void Update () {
MovePlayer();
if (playerHealth < 0) {
playerHealth = 0;
}
}
void MovePlayer() {
moveVelocity = moveSpeed * Input.GetAxis("Horizontal");
myBody.velocity = new Vector2(moveVelocity,myBody.velocity.y);
if (Input.GetButtonDown ("Jump")) {
Jump();
}
}
void Jump () {
if (isGrounded) {
isGrounded = false;
myBody.velocity = new Vector2(myBody.velocity.x, jumpForce);
}
}
//collisions
void OnCollisionEnter2D (Collision2D target) {
if (target.gameObject.tag == "ground") {
isGrounded = true;
}
else if (target.gameObject.tag == "enemy") {
playerHealth = playerHealth - 50;
healthBar.value = playerHealth;
}
}
//booster buttons
public void OpenShop () {
shopPanel.SetActive(true);
}
public void Speed () {
speedSelected = true;
}
public void JumpBooster () {
jumpSelected = true;
}
//buy buttons
public void Buy () {
if (speedSelected) {
moveSpeed = moveSpeed * 2;
shopPanel.SetActive(false);
}
else if (jumpSelected) {
jumpForce = jumpForce * 2;
shopPanel.SetActive(false);
}
info.SetActive(true);
StartCoroutine("CloseTextDelay");
jumpSelected = false;
speedSelected = false;
}
//coroutines
IEnumerator ClosePanelDelay () {
yield return new WaitForSeconds (1);
shopPanel.SetActive(false);
}
IEnumerator CloseTextDelay () {
yield return new WaitForSeconds (1.7f);
info.SetActive(false);
}
}
But I cannot access slider, check this image: http://prntscr.com/9n7liu When I play game, slider is NOT attached as player component, when I attach it before play, and then click play, it immediately disappears from public field! But, when I attach it again (while in play mode), health bar works perfect.
Can someone help me with this issue?
Thanks in advance!
Answer by ClearRoseOfWar · Jan 07, 2016 at 06:06 PM
Im not sure exactly what your problem is... but I have an idea.
this line here:
healthBar = GetComponent<Slider> ();
You've already set it in the inspector by dragging it in there ->
public Slider healthBar;
I think you may be overwriting it at the void Start()
My debug idea, would be one of these two ideas:
remove the line
healthBar = GetComponent<Slider> ();
from your void Start() The reason I suggest this, is cause you've already determind the slider it uses in the inspector.. Maybe your overwriting it? (check next idea for more info)
or try 'this':
healthBar = this.GetComponent<Slider> ();
Maybe im wrong, but someone told me that the new unity doesn't necessitate "this" before using a component... But I always put it there just in case.
If this doesn't help, I'm sorry :) Still getting used to unity myself...
Your answer
Follow this Question
Related Questions
"Clipping", "Jumping" UI slider, attached to the 2d game object ( health bar) 0 Answers
Healthbar/UI Slider glow effect 1 Answer
"Clipping", "Jumping" UI slider, attached to the 2d game object ( health bar) 0 Answers
Change Value Of UI Light Slider to Read At Specific Points Of Slider Value 1 Answer
How can I extend an UI Component and change it's default values when added on editor? 0 Answers