I have a code right now where you can damage the enemy with the Z key, and if the EnemyHealth = 0, then the player gets 72 XP, and the Enemy dies, and respawns 15 seconds later. The problem is, I have to set EnemyHealth = EnemyMaxHealth right after the XP is given, so even if the enemy is “dead” you can still kill it and get XP, because the health is = MaxHealth, though the collider and renderer are disabled. But, if I make the EnemyHealth = 0 right off the bat, then the XP just keeps going up. And up. And up. And up even more until the Enemy respawns. I will post the Enemy stats script, and the player level script.
Enemy Stats(The version where u can kill the dead enemy(Doesn’t flood the XP) (If Health = 0 then it does flood XP))
using UnityEngine;
using System.Collections;
public class EnemyStats : MonoBehaviour {
public int EnemyHealth = 100;
public int EnemyMaxHealth = 100;
public int DropXP = 74; //Enemy won't drop this exact amount...
public int DropGold = 126;
public GameObject GiveStuff;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
PlayerLevelDeclaration PT = (PlayerLevelDeclaration)GiveStuff.GetComponent("PlayerLevelDeclaration");
if(EnemyHealth <= 0){
EnemyHealth = 0;
StartCoroutine(OnDeath());
}
}
IEnumerator OnDeath(){
bool IsDead = false;
PlayerLevelDeclaration PT = (PlayerLevelDeclaration)GiveStuff.GetComponent("PlayerLevelDeclaration");
if(EnemyHealth <= 0){
EnemyHealth = 0;
IsDead = true;
if(PT.DroppedXP >= DropXP){
PT.DroppedXP = 0;
}
if(IsDead == true){
PT.DroppedXP = (int)(DropXP / 2) + PT.DroppedXP;
IsDead = false;
EnemyHealth = EnemyMaxHealth; //Comment this out, (<-) and it increases XP by 72, about 500,000 times.
yield return new WaitForSeconds(.0000000000000000000000001f);
if(DropXP >= DropXP){
PT.DroppedXP = 0;
}
yield return new WaitForSeconds(2f);
collider.enabled = false;
renderer.enabled = false;
IsDead = false;
yield return new WaitForSeconds(10f);
EnemyHealth = EnemyMaxHealth;
PT.DroppedXP = 0;
collider.enabled = true;
renderer.enabled = true;
}
}
yield return IsDead;
}
}
The Playerlevel script
using UnityEngine;
using System.Collections;
public class PlayerLevelDeclaration : MonoBehaviour {
public int PlayerLevel = 1;
public int XP = 0;
public int DroppedXP = 0;
public int NeededXPUntilLevelUp = 100;
public int LeftoverXP = 0;
public int newXP = 0;
public int AddAmount = 50;
public AttributeDeclaration AD;
// Use this for initialization
void Start () {
Debug.Log (LevelUp());
}
// Update is called once per frame
void Update () {
LevelUp ();
AddXP ();
}
public int LevelUp(){
AD = gameObject.GetComponent<AttributeDeclaration>();
XP = XP + DroppedXP;
newXP = XP;
if(XP >= NeededXPUntilLevelUp)
{
//XP = 250, NeededXPUntilLevelUp = 249, then hopefully this sets xp to one, and increases the player level by one
newXP = XP - NeededXPUntilLevelUp;
LeftoverXP = newXP;
PlayerLevel = PlayerLevel + 1;
XP = newXP;
NeededXPUntilLevelUp = NeededXPUntilLevelUp + (int)(PlayerLevel * 3.2f);
//Increase Attributes
AD.Health = AD.Health + (int)(PlayerLevel * 1.9f);
AD.Magic = AD.Magic + (int)(PlayerLevel * 2.1f);
AD.Strength = AD.Strength + (int)(PlayerLevel * 1.6f);
AD.Stamina = AD.Stamina + (int)(PlayerLevel * 1.8f);
AD.Defense = AD.Defense + (int)(PlayerLevel * 1.2f);
//Increase Max Attributes
AD.MaxHealth = AD.MaxHealth + (int)(PlayerLevel * 1.9f);
AD.MaxMagic = AD.MaxMagic + (int)(PlayerLevel * 2.1f);
AD.MaxStrength = AD.MaxStrength + (int)(PlayerLevel * 1.6f);
AD.MaxStamina = AD.MaxStamina + (int)(PlayerLevel * 1.8f);
AD.MaxDefense = AD.MaxDefense + (int)(PlayerLevel * 1.2f);
//Refills current Attributes upon LevelUp
AD.Health = AD.MaxHealth;
AD.Magic = AD.MaxMagic;
AD.Strength = AD.MaxStrength;
AD.Stamina = AD.MaxStamina;
AD.Defense = AD.MaxDefense;
}
XP = newXP;
return PlayerLevel;
}
//Just a test for above code, adds 50 to XP when J is hit.
public int AddXP()
{
#region TestingForFuture
if(Input.GetKeyDown(KeyCode.Q))
{
XP = XP + AddAmount;
}
return XP;
#endregion
}
}