Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Really frustrating issue.

Discussion in 'Scripting' started by BrokenhearT, Jul 6, 2015.

  1. BrokenhearT

    BrokenhearT

    Joined:
    May 18, 2015
    Posts:
    69
    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.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BulletHurtingMonster : MonoBehaviour {
    5.  
    6.     public GameObject anotherbullet;
    7.  
    8.  
    9.  
    10.     void Start ()
    11.     {
    12.    
    13.     }
    14.    
    15.     void Update ()
    16.     {
    17.    
    18.     }
    19.  
    20.     void OnTriggerEnter2D (Collider2D other)
    21.     {
    22.     if (other.tag == "Monster")
    23.         {
    24.             Instantiate(anotherbullet, transform.position,transform.rotation);
    25.             other.GetComponent<MonsterHealthManager> ().DamageMonster(1);
    26.             Destroy (gameObject);
    27.        
    28.         }
    29.     }
    30. }
    31.  



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class MonsterHealthManager : MonoBehaviour {
    6.    
    7.     public int minMonsterHealth;
    8.     public int maxPMonsterHealth;
    9.     public int curMonsterHealth;
    10.  
    11.     public Slider healthbar;
    12.  
    13.     public GameObject monsterexplosion;
    14.    
    15.     public int pointsOnDeath;
    16.  
    17.  
    18.    
    19.    
    20.    
    21.     void Start ()
    22.     {
    23.         curMonsterHealth = maxPMonsterHealth;
    24.     }
    25.    
    26.     void Update ()
    27.     {
    28.         healthbar.value = curMonsterHealth;
    29.        
    30.         if (curMonsterHealth <= 0)
    31.         {
    32.             Instantiate(monsterexplosion, transform.position, transform.rotation);
    33.             ScoreManager.AddPoints (pointsOnDeath);
    34.             Destroy(gameObject);
    35.         }
    36.     }
    37.  
    38.     public void DamageMonster (int damagetogivemonster)
    39.     {
    40.         curMonsterHealth -= damagetogivemonster;
    41.     }
    42.  
    43. }
    44.  
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    The bullet is hitting something that has a tag of "Monster" and yet doesn't have a MonsterHealthManager component.
     
    BrokenhearT likes this.
  3. BrokenhearT

    BrokenhearT

    Joined:
    May 18, 2015
    Posts:
    69
    That fixed it! thank you so much!!!