0

I'm trying to get an instance of another script of mine to call one of it's methods but whatever I try I get this error:

NullReferenceException: Object reference not set to an instance of an object
ZombieBulletHitManager.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/ZombieBulletHitManager.cs:22)

Here is my code for ZombieBulletManager:

using UnityEngine;
using System.Collections;

public class ZombieBulletHitManager : MonoBehaviour {

// Use this for initialization  
void Start () {

}

// Update is called once per frame
void Update () {

}

void OnTriggerEnter2D(Collider2D other) {
    if(other.gameObject.ToString().StartsWith("ZombieBullet")){
    // Add the blood animation.
    spawnBlood();

    // Destroying the game objects
    gameObject.GetComponent<ArcherBulletShootHandler>().removeBullet(other.gameObject);
    Destroy (other);
    Destroy(gameObject);
    }
}   

private void spawnBlood(){
    GameObject go = Instantiate(
        Resources.Load<GameObject>("ZombieBlood"),
        transform.position,
        Quaternion.identity) as GameObject;
     }
}

EDIT: Apparently this is a duplicate however it's not, I have no idea where is null on the line gameObject.GetComponent().removeBullet(other.gameObject);

Any help would be great, thank you!

Nicster15
  • 159
  • 3
  • 12
  • Well, which line of code does it break to? One of your objects is null, either other = null, or gameObject = null, or gameObject.GetComponent() = null. – Jon Jul 19 '16 at 21:37
  • @Mangist the line with gameObject.GetComponent(). How could these be null if I have the script, the gameObject was made when the class was created, and other is a paremeter in the event. EDIT: Looks like gameObject.GetComponent() is null, but how? – Nicster15 Jul 19 '16 at 21:49
  • Look at the API documentation, it returns null if that game object doesn't have that component attached. https://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html – Jon Jul 19 '16 at 21:56
  • Brake it up to 3 lines, `var component = gameObject.GetComponent(); var other= other.gameObject; component.removeBullet(other);` however I suspect it is the 3nd line. Whatever `gameObject` is, it does not have a `ArcherBulletShootHandler` attached to it, if the class is not attached then GetComponent returns null. – Scott Chamberlain Jul 19 '16 at 21:57
  • 1
    Also *"Apparently this is a duplicate however it's not, I have no idea where is null on the line ..."* **The duplicate teaches you how to figure that information out.** you really need to go read through the answers and understand what the problem is and how to debug it to gain more information like which object is null. – Scott Chamberlain Jul 19 '16 at 22:02

0 Answers0