- Home /
Can't find source of trigger Null Reference Exception
I have a box (non-trigger, kinematic rigidbody) that passes through different triggers (also set to kinematic rigidbody). The programming seems to function fine, but I keep getting a Null Reference Exception error, even though I've checked several times to make sure all Inspector-set variables have been dragged and dropped in place, and the right tags and scripts are assigned to the objects. Two of my three trigger types work fine ("slicks" and "clouds" are good, "gates" give the error), but they are set up the same way and compiling returns no errors.
The problem line is in the box script: if (triggerEnter.GetComponent().isActive == true)
Commenting out the line also makes the errors disappear.
Tests to check that something is returned on triggering come back correct, so I don't understand why I'm getting this error. I'm still new to C#. Does anyone know what the problem could be? Here are the scripts:
BOX SCRIPT
using UnityEngine;
using System.Collections;
public class Box_Script : MonoBehaviour {
public bool normalTimeSpeed = true; // has the time scale been changed?
public float timeFast = 1.5f; // controls time scale alterations
public float timeSlow = 0.75f;
//========================================
void Update ()
{
// GATE TRIGGERS
void OnTriggerExit(Collider triggerObject)
{
if(triggerObject.tag == "Gate" && triggerObject.GetComponent<GatesGeneral>().isActive == true)
{
print (triggerObject.GetComponent<GatesGeneral>().isActive);//TEST. PRINTS "TRUE" LIKE IT SHOULD
triggerObject.GetComponent<GatesGeneral>().isActive = false; // prevent repeated triggering
}
}
// TIME CONTROL TRIGGERS
void OnTriggerEnter(Collider triggerEnter)
{
if (triggerEnter.GetComponent<Speeders>().isActive == true) // PROBLEM LINE
{
if(triggerEnter.tag == "Slick") // Slicks speed up time
{
triggerEnter.GetComponent<Speeders>().isActive = false; // prevent repeated triggering
if (normalTimeSpeed == true) // time is normal
{
Time.timeScale = timeFast; // speed up time
normalTimeSpeed = false;
}
else // time was already slow
{
Time.timeScale = 1; // return to normal
normalTimeSpeed = true;
}
}
else if(triggerEnter.tag == "Cloud") // Clouds slow down time
{
triggerEnter.GetComponent<Speeders>().isActive = false; // prevent repeated triggering
if (normalTimeSpeed == true)
{
Time.timeScale = timeSlow;
normalTimeSpeed = false;
}
else
{
Time.timeScale = 1;
normalTimeSpeed = true;
}
}
}
}
}
GATE SCRIPT
using UnityEngine;
using System.Collections;
public class GatesGeneral : MonoBehaviour {
public bool isActive = true; // Item is active. Set by Box script to block repeated triggering (such as for speed modifiers)
public Material gateNormalRef; // normal gate material (this variable for Lerp)
public Material gateYesRef; // material for a gate that has been successfully passed thru
private float timer = 0; // for Lerp
void Update()
{
if (tag == "Gate" && isActive == false) // Item tagged Gate has been collided with
{
renderer.material.Lerp (gateNormalRef, gateYesRef, timer); // turn green
timer += (3.5f * Time.deltaTime);
}
}
}
CLOUD/SLICK SCRIPT
using UnityEngine;
using System.Collections;
public class Speeders : MonoBehaviour {
public bool isActive = true; // Item is active. Set by Box script to block repeated triggering (such as for speed modifiers)
public Material slickRef;
public Material cloudRef;
public Material nullRef;
private float timer = 0; // for Lerp
void Update ()
{
// MATERIAL LERPS
if (tag == "Slick" && isActive == false)
{
renderer.material.Lerp (slickRef, nullRef, timer); // fade out
timer += (0.25f * Time.deltaTime);
}
if (tag == "Cloud" && isActive == false)
{
renderer.material.Lerp (cloudRef, nullRef, timer); // fade out
timer += (0.75f * Time.deltaTime);
}
}
}
Answer by coastwise · Dec 03, 2012 at 03:40 AM
I'm guessing you have some other object with a trigger collider that isn't a cloud (doesn't have a Speeders attached). This could be your track, finish line, anything really. This shouldn't be a major concern, and you can solve your null reference error by checking to see if the triggered object is the type you are looking for.
if (triggerEnter.GetComponent<Speeders>() != null && triggerEnter.GetComponent<Speeders>().isActive == true) {
That worked great and now I realize what I did. The Gate part of the Box script works fine because I checked the triggering object for the Gate tag, before using GetComponent on it. The Clouds and Slicks section checked for tags after GetComponent, so if I hit a Gate, it registers like any other trigger which means GetComponent is called on it, but the console complains due to the lack of the attached Speeder script.
Thanks!