- Home /
Issue with OnTriggerEnter.
So for some reason my script won't work and I can't figure out why. It is meant to show and hide a plane depending if its in the water or not. Thanks in advance for your help.
#pragma strict
var Showing : boolean;
function Start () {
renderer.enabled = false;
Showing = false;
}
function Update () {
DisableRendererWithDelay();
}
function OnTriggerEnter (other : Collider) {
if (other.CompareTag("Water"))
{
Showing = true;
}
}
function DisableRendererWithDelay() {
if (Showing == true)
{
renderer.enabled = true;
yield WaitForSeconds(5);
renderer.enabled = false;
}
}
Answer by b1gry4n · Nov 06, 2014 at 07:29 AM
Before I start, I am not very familiar with javascript.
Calling DisableRendererWithDelay in update probably isnt the best thing to do. If showing = true, you are calling that function (which is essentially a coroutine) every frame.
This is how I would do it:
function OnTriggerEnter (other : Collider) {
if (other.gameObject.tag == "Water") && !Showing)
{
DisableRendererWithDelay(true);
}
}
function OnTriggerExit (other : Collider) {
if (other.gameObject.tag == "Water") && Showing)
{
DisableRendererWithDelay(false);
}
}
function DisableRendererWithDelay(shouldShow : boolean) {
yield return new WaitForSeconds(5.0f);
Showing = shouldShow;
}
Keep in mind, if the player goes in and out of the water. Every time they do that the OnTrigger functions will be called. This will stack the coroutine over and over which will probably cause some flickering. You will need to implement something to control that
Thanks this worked after knocking some errors out of it. Thank you.
Your answer
Follow this Question
Related Questions
Setting Mesh Colliders convex through script 1 Answer
OnCollisionEnter2D not working? 2 Answers
Script error: OnTriggerEnter 1 Answer
Javascript error.. insert semicolon?? 2 Answers