A script that check if the player lost or gain HP

Hey, i have a life system and i need to make a script that instantiate a 3d text with the health number that i have lost or gain, but i dont really know how to check if the player lost or gain health, that is the script: (its in C# but i use javaScript more, so C# or javaScript are both ok for me).

using UnityEngine;
using System.Collections;

public class playerHealth : MonoBehaviour {
	public int maxHealth = 100;
	public int curHealth = 100;
public GUISkin _theSkin = null;
	
	public float healthBarLength;

	void Start () {
	healthBarLength = Screen.width / 2;
	}
	
	void Update () {
	AddjustCurrentHealth(0);
	}
	void OnGUI(){
if (_theSkin != null) 
        { 
            GUI.skin = _theSkin; 
        } 
		GUI.Box(new Rect(10,10, healthBarLength, 20), curHealth + "/" +maxHealth);
	}
		public void AddjustCurrentHealth(int adj){
			if(curHealth < 0)
				curHealth = 0;
			
			if(curHealth > maxHealth)
				curHealth = maxHealth;
			
			if(maxHealth < 1)
				curHealth = maxHealth;
			
			healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
		}

	}

i have found it in a tutorial series, and i have made a font + a 3d text prefab with animations and everything but all i need now is the check when the player lose or gain health and instantiate the 3d text prefab with the number of the health loss or gained.

since you use the AddjustCurrentHealth function, you can make a variable that indicates damage to the attackscript:

public int curDamage;

void Attack () {
   AddjustCurrentHealth(curDamage);
}

void OnGUI () {
  GUI.Box(new Rect(50, 50, 70, 30), "" + curDamage);
}

is there a way just to check how much health i got now compared to the health i had before i got hit? or got healed?

Bumping
still need help…

You’ll probably just have to store somewhere the changes as they happen.

How do i do that?
sorry im just newb…

:smile:

i wish it was that easy, im making an rpg style game and i have like 100 hp and everytime i get hit, i lose a random number between 5 - 20 hp so i need to make nubers that goes up the player and disapear saying how much hp i have loss in red and how much hp i have gained (from skills and etc) in blue.

A number popping, showing the damage taken…it is triggered by a function, right ?

When your character lose life, you call that function.

You simply give the function the correct infos (by passing an argument, the life quantity lost, for example), while executing it.

thers a player script, if the player is hitting a fallout tagged mesh, it calls the curDamage (from the script of the hpbar) says: curdamage - Random.range(5,20);

Well, you simply call at the same time the function visually popping the damage on your character (instantiating a 3D Text, all that stuff), by giving it the value to display.

wait, if i call it in the same time, it will have the same random number? O_O ?

When you call Random.Range (x, y), it returns you a random value between x and y. If you call it again, you will get another value. So, you must temporarily store the first returned value, and work with it.

As simple as that :slight_smile:

thanks that makes sense, ill try that, but i have another question, can i make it like this?
make an int var called x
and say
if i hit a fallout tagged object, x = random.range(5,20) and than curHealth = curHealth-x.
and reset x everytime i use it?
and instead of using curHealth, ill use x for instantiating.
does that makes sense?

Whenever you assign a value to x (like, x = Random.Range), you overwrite the previous value, so there is no real need to “reset” it (unless special circumstances).

Since x is the life lost, you will work with x, when you want to display the life lost.

DisplayLostLife (lifeLost : int)
{
   // stuff
}

DisplayLostLife (x);

Which is almost exactly what I meant :stuck_out_tongue:

alright!
one last question, is there a way or a function or something that check if the variable has changed? like from 13 to 5 to 3 to 2 so i could say, if that changes, instantiate a prefab with the text of the variable x.
thanks :wink:

You will have to use a second variable.

previousValue
currentValue

if currentValue not equal previousValue, store currentValue into previousValue.

Thanks for everything :wink: you rock