Help with my PopUp Damage Text!
Hello, I have a problem with my script to display a text indicating the damage that the player made an enemy , but I checked in and not be wrong . When testing the script I get the error " NullReferenceException : Object reference not set to an instance of an object" . The scripts are as follows :
Health: This script is attached to the player and enemies
public int HP = 5;
public int HPMaximo = 5;
public GameObject PrefabTextoDamagePopUp;
public List<GameObject> TextoDamagePopUpLista = new List<GameObject>();
int indiceTextoDamagePopUpListaActual = 0;
public void doDamage(int Damage){
HP -= Damage;
TextoDamagePopUpLista[indiceTextoDamagePopUpListaActual] = (GameObject)Instantiate
(PrefabTextoDamagePopUp,
new Vector3
(transform.position.x, transform.position.y + 2, transform.position.z),
Quaternion.identity);
indiceTextoDamagePopUpListaActual++;
TextoDamagePopUpLista[indiceTextoDamagePopUpListaActual].GetComponent<TextDamagePopUp>().DamageMostrado = Damage;
}
Arma: This script is attached to the weapon of the player
public Sprite[] SpriteArray;
public int[] DamageArray;
public bool ArmaActiva = false;
public static int ID;
public static int DamageActual = 2;
void OnTriggerEnter2D(Collider2D coll){
if(ArmaActiva && coll.GetComponent<Health>() && coll.tag != "J"){
coll.GetComponent<Health>().doDamage(DamageActual);
}
}
}
TextDamagePopUp: This script is attached to the 3d text prefab , which is going to show the damage that the player has made
public float tiempoVida;
float tiempoVidaA;
public int DamageMostrado;
void Start(){
tiempoVidaA = tiempoVida;
}
void FixedUpdate(){
tiempoVida -= Time.fixedDeltaTime;
GetComponent<TextMesh>().text = System.String.Empty + DamageMostrado;
if(tiempoVida <= 0)
Destroy(gameObject);
}
}
If there is another way to show the damage done in a text, they will thank you if I explain it please! alt text
You need to post the complete class file(s) involved. You also need to post a little more than NullReferenceException. Place a comment in your code where the error occurs like this
//<---- error here
NullReferenceException: Object reference not set to an instance of an object
Health.doDamage (Int32 Damage) (at Assets/Scripts/Health.cs:20)
Arma.OnTriggerEnter2D (UnityEngine.Collider2D coll) (at Assets/Scripts/Arma.cs:18)
The lines of the corresponding error are the following:
Health.cs:20 TextoDamagePopUpLista[indiceTextoDamagePopUpListaActual].GetComponent<TextDamagePopUp>().Damage$$anonymous$$ostrado = Damage;
Arma.cs: 18
coll.GetComponent<Health>().doDamage(DamageActual);
Your answer
Follow this Question
Related Questions
[Solved]unexplanable null refrence exception 4 Answers
Inventory problem 0 Answers
Null Reference Exception on C# script 0 Answers
Any idea why this is throwing a NullReferenceException ? 2 Answers