- Home /
Getting XP when dies Help
Hello! :) I have 2 Scripts. One for the "Stats" and one for "The Enemy". I want so when an Enemy Dies It Gives XP to the Player. How can i do that? Any Ideas? :)
SCRIPT:
using UnityEngine; using System.Collections;
public class EnemyHealth : MonoBehaviour { public int maxHealth; public int curHealth; public int GiveXP; public GameObject destroy;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
AddjustCurrentHealth(0);
}
public void AddjustCurrentHealth(int adj) {
curHealth += adj;
if(curHealth <= 0)
Die();
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1;
}
public void Die () {
curHealth = 0;
Destroy(destroy);
}
}
Presu$$anonymous$$g your player has an XP field or property, you can hold a reference to the player in the enemy's script and add XP directly or by calling a method on the player.
Answer by Nahoyman78 · Apr 22, 2012 at 12:30 PM
I Tried This way:
using UnityEngine; using System.Collections;
public class EnemyHealth : MonoBehaviour { public int maxHealth; public int curHealth; public int giveXP; public GameObject destroy;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
AddjustCurrentHealth(0);
}
public void AddjustCurrentHealth(int adj) {
curHealth += adj;
if(curHealth <= 0)
Die();
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1;
}
public void Die () {
curHealth = 0;
Destroy(destroy);
Stats st = (Stats)GetComponent("Stats");
st.XP += giveXP;
}
}
Any Ideas on this Problem?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Getting XP Script Help 2 Answers
Stats effecting the max Health Value 2 Answers
new user very easy c# question regarding syntax 1 Answer
Die Function Help With C# 1 Answer