Sorry in advance ,I looked all day in Unity Answers and on the forums but could not find the answer I’m looking for.
All I want to do is activate a script that is attached to an object when the player enters the trigger, and disable it when the player leaves the trigger.
This is the script I am tying to use to enable the script:
using UnityEngine; using System.Collections;
public class scripttrigger : MonoBehaviour {
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == “Player”)
{
ConstantRotate ConstantRotate = other.gameObject.GetComponent();
ConstantRotate.enabled = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == “Player”)
{
ConstantRotate ConstantRotate = other.gameObject.GetComponent();
ConstantRotate.enabled = false;
}
}
}
However I keep getting this error message :
NullReferenceException: Object reference not set to an instance of an object
scripttrigger.OnTriggerEnter (UnityEngine.Collider other) (at Assets/scripttrigger.cs:12)
I have the “scripttrigger” script attached to a cube which is set as a trigger and the object is parented to the trigger cube. The “ConstantRotate” script is attached to both the cube and the object and unchecked/disabled on both.
Can anyone please help me figure out what I’m doing wrong?