Question by
Lunehtik · Apr 10, 2016 at 02:05 AM ·
gameobjectdestroyvariable
No error code? Not sure where to go?
When the "Enemy" hits 0 hp the game object deletes itself just like I want, but it does not add the experience to the player. I'm not getting an error code which has left me not sure what I'd search for to fix this issue.
Enemy.cs
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour
{
Player player;
public UnityEngine.UI.Text EnemyDisplay;
public int enemyhp = 10;
public int enemydef = 1;
public int enemybexp = 50;
void Start()
{
player = gameObject.AddComponent<Player>();
}
void Update()
{
EnemyDisplay.text = "HP: " + enemyhp.ToString();
// You need to convert your int to a string to add it to the button text
if (enemyhp <= 0)
{
player.pbaseexp += enemybexp;
Destroy(gameObject);
}
}
public void OnMouseDown()
{
enemyhp -= player.pattack - enemydef;
}
}
Player
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public UnityEngine.UI.Text PBaseLevelDisplay;
public UnityEngine.UI.Text PJobLevelDisplay;
public UnityEngine.UI.Text PBaseExpDisplay;
public UnityEngine.UI.Text PJobExpDisplay;
public UnityEngine.UI.Text PHealthDisplay;
public UnityEngine.UI.Text PAttackDisplay;
public UnityEngine.UI.Text PDefenseDisplay;
public UnityEngine.UI.Text PTClicksDisplay;
public int pbaselevel = 1;
public int pjoblevel = 1;
public int pbaseexp = 0;
public int pjobexp = 0;
public int phealth = 10;
public int pattack = 2;
public int pdefense = 1;
public int ptclicks = 0;
void Update()
{
PBaseLevelDisplay.text = "Base Level: " + pbaselevel;
PJobLevelDisplay.text = "Job Level: " + pjoblevel;
PBaseExpDisplay.text = "Base Exp: " + pbaseexp;
PJobExpDisplay.text = "Job Exp: " + pjobexp;
PHealthDisplay.text = "Health: " + phealth;
PAttackDisplay.text = "Attack: " + pattack;
PDefenseDisplay.text = "Defense: " + pdefense;
PTClicksDisplay.text = "Total Clicks: " + ptclicks;
}
}
Comment
Best Answer
Answer by Zoogyburger · Apr 10, 2016 at 02:55 AM
In start you put
player = gameObject.AddComponent<Player>();
You are not trying to add a component instead put
player = FindObjectOfType<Player> ();