Hello,
Unity is throwing Null Reference Exceptions at run time. The kicker is that everything seems to be working as intended in terms of game play. What’s going on here? My script is below. Thanks in advance!
Note: this script is attached to a prefab that gets instantiated at run time. The public GameObject references (particle1 -4) are also Prefabs that have been assigned via drag and drop in the editor. The console says my problem is on line 24.
using UnityEngine;
using System.Collections;
public class BallManager : MonoBehaviour
{
public GameObject explosion;
public GameObject particle1;
public GameObject particle2;
public GameObject particle3;
public GameObject particle4;
GameObject _instantiator;
void Start ()
{
_instantiator = GameObject.Find ("Ball_Instantiator");
}
void OnTriggerStay (Collider controllerCol)
{
if (controllerCol.gameObject.GetComponent<InteractionManager> ().triggerDown == true)
{
Debug.Log ("Start Fade");
particle1.GetComponent<Additive_Shader_Fade_Out_Curve> ().enabled = true;
particle2.GetComponent<Additive_Shader_Fade_Out_Curve> ().enabled = true;
particle3.GetComponent<Additive_Shader_Fade_Out_Curve> ().enabled = true;
particle4.GetComponent<Additive_Shader_Fade_Out_Curve> ().enabled = true;
}
}
void OnTriggerEnter (Collider col)
{
if (col.gameObject.tag == "Boundary")
{
_instantiator.GetComponent<SpawnManager> ().ballDestroyed = true;
Instantiate (explosion, transform.position, transform.rotation);
Destroy (this.gameObject);
}
}
}
Again, everything executes perfectly at run time, so how could that be if my references were null? Puzzled.