Health Bar Problem
I have been trying to implement a health bar that decreases when my player is damaged, though no matter which method I use, the image doesn't scale down, it stays the same.
These are the tutorials I have tried and none have worked: https://www.youtube.com/watch?v=qbg58-l8v6Q https://www.youtube.com/watch?v=ox-QiHfUqdI https://www.youtube.com/watch?v=cV5FwwEVjw4 https://www.youtube.com/watch?v=5KwkfGfaRNU
This is the current code I am using:
using UnityEngine;
using System.Collections;
public class PlayerHealth : MonoBehaviour {
public float Health = 100f;
private SpriteRenderer healthbar;
private Vector3 healthscale;
void start(){
healthbar = GameObject.Find ("HealthBar").GetComponent<SpriteRenderer> ();
healthscale = healthbar.transform.localScale;
}
void update(){
if (Health <= 0) {
Application.Quit ();
Debug.Log ("The game should quit right now.");
}
if (Health > 100) {
Health = 100;
}
HealthUpdate ();
}
void HealthUpdate(){
healthbar.transform.localScale = new Vector3 (healthscale.x * Health * 0.01f, 1, 1);
}
}
Answer by Anton-Korhonen · Nov 01, 2016 at 02:29 PM
Change your start and update function to Start and Update (first letter uppercase)
Oh wow! It suddenly works! I didn't know having them capitalized would be changing the code entirely. Thank you for the answer! Should this be the problem why my other codes aren't working then? :)
It definitely could be. Like variables, all functions are case-sensitive. Happy coding!
I will keep that in $$anonymous$$d! Thanks again! :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612095825im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make a shooting bow? 0 Answers
Scripts Interfering With Each Other 1 Answer
Accessing string from other script 1 Answer
Unity {[(2D)]} RotateAround while jumping 0 Answers
Unexpected token: if HELP 1 Answer