Hey, I’ve been working at this error for a while now but can’t seem to figure it out. I’m getting a NullReferenceException when the player (ball) collides with a trigger. I do have the GameObjects referenced in the inspector, and I have tried putting “col != null” in the if statements. So I’m stuck. Haha I really hope I missed something obvious.
Any help would be greatly appreciated. Thanks in advance.
The exact error I’m getting is this:
NullReferenceException: Object reference not set to an instance of an object
GateCollision.OnTriggerEnter2D(UnityEngine.Collider2D col)(at Assets/Scripts/GateCollision.cs:25)
Here’s the script where I’m getting the error:
(When I double click on the error it highlights the open curly brace that’s right after the first if statement)
using UnityEngine;
using System.Collections;
public class GateCollision : MonoBehaviour
{
public GameObject ball;
public GameObject gateSpawnScript;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
void OnTriggerEnter2D (Collider2D col)
{
if(col.gameObject.tag == "DestroyGateCol" && col.GetComponent<GateLogic>().isActiveGate == true)
{
gateSpawnScript.GetComponent<GateSpawning> ().SpawnGate();
col.GetComponent<GateLogic>().destroyGate();
}
else if(col.gameObject.tag == "DestroyPlayerCol")
{
//play ball destroying animation
Destroy(ball);
}
}
}