OnTriggerEnter and Light on
Hello,
I'm a beginner in Unity programming and I'm actually making my first 3D platform game.
I created a trap platform. When you walk on it, it falls and you are send back to the start position. I've been able to put a song on it to let the player know there is something particular on it, but I wanted to add also a visual effect to help if the player has no sound.
Then I created a spot light, that I put up the platform. I have a script for for the fall down platform :
public class CubeFallDown : MonoBehaviour {
// Variables
public float TimeToWait = 1f;
public Light RedLight;
private Rigidbody rb;
private Vector3 initialPosition;
private Material plateformColor;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
initialPosition = transform.position;
plateformColor = GetComponent<Renderer>().material;
RedLight.enabled = false;
}
// Update is called once per frame
void Update () {
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
RedLight.enabled = true;
plateformColor.color = Color.red;
GetComponent<AudioSource>().Play();
StartCoroutine(FallDown());
RedLight.enabled = false;
}
But when I play the game it doesn't work. And it tells me that the RedLight variable has an unassigned reference exception, and I don't understand the issue as I put my spot light in the public variable of the platform in Unity designer so, it should be initiated... so if somebody can tell me where I'm wrong and maybe explain me why... Thanks in advance !
Answer by Djoul82 · Apr 16, 2018 at 06:07 PM
It works, It's just a question of turning light off too fast...
But I still have the unassigned reference exception on RedLight variable... what do I need to do to manage this error ?
EDIT : OK found... I used my script for another platform somewhere else in the project and I didn't assign a light to it. Now it's ok !
Your answer
Follow this Question
Related Questions
Game Over when character enters in lighted area 1 Answer
Lights shutting down with sound 1 Answer
How to use light Range or intensity as a trigger? 0 Answers
How do I detect a collision between two objects using Bolt? 1 Answer
How to activate a trigger in state machine when object reaches position? 0 Answers