i have no clue what is wrong with the script (help)
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Healthtry : MonoBehaviour { public int Health;
public Text HealthBar;
public gameObject DeathGUI;
void Death()
{
if (Health <= 0)
{
DeathGUI.SetActive(true);
}
}
void Start()
{
Health = 10;
HealthBar.text = Health.ToString();
DeathGUI.SetActive(false);
}
void HealthTimer()
{
Health--;
HealthBar.text = Health.ToString();
}
void Damage()
{
Health = -1;
}
void Update()
{
if (Input.GetMouseButtonDown(0) && (hit.transform.gameObject.name == "Damage")) {
Damage();
}
void Kill()
{
Xp();
TimeXp();
}
void health()
{
if (hit.transform.gameObject.name == "HealthPack")
{
Health += 5;
}
}
void TimeXp()
{
Float Time = 5;
Time -= deltaTime;
}
void Xp()
{
if (Time <= 0)
{
Xp.SetActive(false);
}
else if ()
{
Xp.SetActive(true);
}
}
}
Comment
Answer by Genome · Aug 27, 2017 at 04:14 PM
public Text HealthBar;
public gameObject DeathGUI;
// set Health and Time to be class scope variables
Health = 0;
Time = 0;
void Start()
{
Health = 10;
Time = 5;
HealthBar.text = Health.ToString();
DeathGUI.SetActive(false);
}
void Update()
{
if (Input.GetMouseButtonDown(0) && (hit.transform.gameObject.name == "Damage")) // Assuming you have a section of code somewhere that handles collision data and are saving it to a variable named hit?
{
Damage();
}
Time -= Time.deltaTime; // This is the way to call deltaTime and subtract it from a variable;
}
//void HealthTimer(){Health--;HealthBar.text = Health.ToString();} // Removed function since it wasn't being called in Update, meaning it wasn't doing anything
void Damage()
{
Health -= 1; // You were setting the HP to -1 every frame the game was playing by saying (Health = -1) and calling this function in Update
HealthBar.text = Health.ToString(); // Added this to display health after taking damage
// Call our modified isDeath Function to see whether or not that damage killed us
isDeath();
}
void Kill()
{
// See if we gain xp from this
Xp();
//TimeXp(); // Removed this function, read comment in function section
}
void health()
{
if (hit.transform.gameObject.name == "HealthPack") // I assume there is more code to this script?? What I mean is that you aren't just assuming you can call collision data without setting it to a variable first?
{
Health += 5;
}
}
//void TimeXp(){Float Time = 5;Time -= deltaTime;} // Commented this function out as moving the variable to the class's scope and changing it in the update function seemed to be more appropriate
void Xp()
{
if (Time <= 0) // You were attempting to access a variable that didn't exist in the scope of this function by trying to test "Time", Will work now that Time is a class scope variable
{
Xp.SetActive(false); // Again I'm assuming there is information left out as I would take it you are setting a gameobject or script to false? Otherwise you may as well just do nothing if time < 0 and call a function from that script if you're giving them xp
}
else // Changed to else{} because you had it checking for IF NOTHING by doing "else if(){...}"
{
Xp.SetActive(true); // Same as above
}
}
// Modified the function to logically make sense in the name, and to call the kill function if we died.
void isDeath()
{
if (Health <= 0)
{
DeathGUI.SetActive(true);
Kill();
}
}
}
There is a lot that is/was wrong with this, take the time to read over the comments because it will definitely help.
Assuming this was a 1-1 copy and paste from your class file the syntax was all messed up, all your functions were in the update function which never would've worked. A lot of calls are/were being made to variables that weren't part of that functions scope at that time.