Checking for a float as a health bar
Hey all I am new to coding and I am working on a new level I have used this code to show on screen the players health. Is there a way where when the health hits no Health that I respawn at a set location? any help would be great thanks all in advance
using UnityEngine; using System.Collections;
public class Health : MonoBehaviour {
GameObject box; //variable collectable box
public float health = 100; // variable for score floated to the screen/publically
//use this for initialization
void start()
{
box = GameObject.FindWithTag("Health");
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Health")
{
health -= 5;
Destroy(other.gameObject);
}
}
void OnGUI()
{
GUI.Label(new Rect(150, 160, 300, 300), "Health : " + (int)health);
}
void Update()
{
}
}
Answer by Makri907 · Nov 08, 2016 at 04:21 PM
Yes, you can do something like this:
GameObject box; //variable collectable box
//Add a transform for your spawn position
Transform spawn;
//Add height you want to spawn at
public float spawnHeight;
public float health = 100; // variable for score floated to the screen/publically
//Add variable to save the initial health
float savedHealth;
//use this for initialization
void start()
{
//Assign health to saveHealth
saveHealth = health;
//Find the spawn object
spawn = GameObject.Find("Spawn").GetCompnent<Transform>();
box = GameObject.FindWithTag("Health");
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Health")
{
health -= 5;
Destroy(other.gameObject);
}
}
void OnGUI()
{
GUI.Label(new Rect(150, 160, 300, 300), "Health : " + (int)health);
}
void Update()
{
//Check if health is zero
if(health<=0){
Vector3 pos = new Vector3(spawn.position.x,spawn.position.y,spawn.position.z);
pos.y = spawn.position.y + spawnHeight;
transform.position = pos;
//Reset Health
heath = saveHealth;
}
}
}
Tell me if this works. I am not at my computer so I can't test it myself.
Note: Make a gameObject in the editor and name it Spawn or whatever you say it is in, spawn = GameObject.Find("[insert your gameobject name]").GetComponent();
Hey I looked at the code and it seamed okay I have just put it to use and I am getting an error image as you can see what unity is saying rather than me trying to explain the problem its talking about the name space will upload the image when I get home
I tried this code and didn't get an error:
GameObject box; //variable collectable box
//Add a transform for your spawn position
public Transform spawn;
//Add height you want to spawn at
public float spawnHeight;
public float health = 5; // variable for score floated to the screen/publically
//Add variable that will be used to reset health
public float savedHealth;
//use this for initialization
void start()
{
//Find the spawn object
box = GameObject.FindWithTag("Health");
}
void OnCollisionEnter(Collision other)
{
if (other.collider.tag == "Health")
{
health -= 5;
Destroy(other.collider.gameObject);
}
}
void OnGUI()
{
GUI.Label(new Rect(150, 160, 300, 300), "Health : " + (int)health);
}
void Update()
{
if (health <= 0) {
SpawnPlayer ();
}
}
void SpawnPlayer(){
if (health != 0) {
return;
}
Vector3 pos = new Vector3 (spawn.position.x, spawn.position.y, spawn.position.z);
pos.y = spawn.position.y + spawnHeight;
transform.gameObject.GetComponent<Rigidbody> ().transform.position = pos;
//Reset Health
health = savedHealth;
}
}
Note: Just assign the spawn object, the savedhealth, the health, and spawn height, in your inspector. Savedhealth should be the value you want your health to be when your player re-spawns.
maybe you left out the headers: using UnityEngine; using System.Collections; public class Health : $$anonymous$$onoBehaviour { } Note: the code should be nested in the class.
Your answer
Follow this Question
Related Questions
Do you need to have specific script types for different mobile platforms? 1 Answer
Making a potion class (change values two objects down) 0 Answers
NullReferenceException by detecting health 1 Answer
What is wrong with my code...watched an unity tutorial and I keep forgetting everything! 1 Answer
Hearth based health system help 0 Answers