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;
	 }
 }

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