- Home /
Getting XP Script Help
Hi Guys :) I have a Problem. I have a script that shall give the Player XP when the Enemy Dies. but it doesn't give any XP. Any Solutions :)? 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 () {
Stats st = (Stats)GetComponent("Stats");
st.XP += giveXP;
curHealth = 0;
Destroy(destroy);
}
}
I'm trying to understand here. The stats script is also on the enemy game object, and the player get the xp from the stats script on the enemy game object?
Correct?
You know, you should make stats a static class, than you wouldn't have to worry about accessing it's instance but access it's properties directly via the class like this:
Stats.xp += givexp;
Answer by fafase · Apr 22, 2012 at 05:49 PM
You could simply put on the enemy
function Start(){
var player = GameObject.Find("Player");
stats : Stats = player.GetComponent("Stats");}
function Update(){
if(dies)
stats.exp+=100;
}
Or as your exp is a single value, you can use static
on the player Stats.js
static var exp;
on the enemy
function Update(){
if(dies)
Stats.exp +=100;}
Answer by Artifacta · Apr 22, 2012 at 09:39 PM
So I guess your player has the stats script on him?
Try
Stats st = Gmeobject.Find("Player").GetComponent<Stats>();
st.XP += giveXP;
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Getting XP when dies Help 1 Answer
NullReferenceException 1 Answer
Distribute terrain in zones 3 Answers
Error (Read Only) 1 Answer