Hello guys, I’m sorry that I’m posting so many issues, but I would like someone to have a look on that script and tell me what’s wrong with it.
This is the error
NullReferenceException: Object reference not set to an instance of an object
BulletHurtingMonster.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/BulletHurtingMonster.cs:25)
and this is the code, and I’ll link the code of the MonsterHealth below it.
using UnityEngine;
using System.Collections;
public class BulletHurtingMonster : MonoBehaviour {
public GameObject anotherbullet;
void Start ()
{
}
void Update ()
{
}
void OnTriggerEnter2D (Collider2D other)
{
if (other.tag == "Monster")
{
Instantiate(anotherbullet, transform.position,transform.rotation);
other.GetComponent<MonsterHealthManager> ().DamageMonster(1);
Destroy (gameObject);
}
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class MonsterHealthManager : MonoBehaviour {
public int minMonsterHealth;
public int maxPMonsterHealth;
public int curMonsterHealth;
public Slider healthbar;
public GameObject monsterexplosion;
public int pointsOnDeath;
void Start ()
{
curMonsterHealth = maxPMonsterHealth;
}
void Update ()
{
healthbar.value = curMonsterHealth;
if (curMonsterHealth <= 0)
{
Instantiate(monsterexplosion, transform.position, transform.rotation);
ScoreManager.AddPoints (pointsOnDeath);
Destroy(gameObject);
}
}
public void DamageMonster (int damagetogivemonster)
{
curMonsterHealth -= damagetogivemonster;
}
}