How do I call for a Visible or Invisible Water?

Hey guys ! I doing a simple code with Javascript, that check if Character’s “Y” Position is bigger than my Water’s “Y” Position.

If yes. Then my water will disappear, if not, my water will stay normal. Look at my code:

#pragma strict

var waterLevel : float;
private var isUnderWater : boolean;

function Start () {

}

function Update () {

	var altura_jogador : float = GameObject.Find("First Person Controller").GetComponent(transform.position.y);
		if (altura_jogador < waterLevel){
		//Daylight Water2 is my Day Light name!
	GameObject.Find("Daylight Water2").enabled = true;
		}else{
	GameObject.Find("Daylight Water2").enabled = false;	
		}
}

That code doesn’t works. Does anybody can help me?

Sorry for my BAD english -.-’ I from germany.

Ok, first of all, you shouldn’t search for any gameobjects every fraem during Update(). Do it once in Start() and then you can access the transform.position.y variable in Update(). GameObject.find is fairly costly in terms of performance.

To enable or disable whole gameObjects, use SetActive(false).

.enable = false is only for Components such as scripts on a gameobject. Be aware that you won’t be able to “Find” gameobjects if they are currently inactive.

The GetComponent function is used to access components, not for direct variable access. Your code would need to be

 var altura_jogador : float = GameObject.Find("First Person Controller").gameObject.transform.position.y;

But I would go one step further and write the code like this:

#pragma strict

    var waterLevel : float;
    private var isUnderWater : boolean;
    var playerObject : GameObject;
    var altura_jogador : float;
var DaylightWater : GameObject;
     
    function Start () {
      playerObject  = GameObject.Find("First Person Controller").gameObject;
DaylightWater  = GameObject.Find("Daylight Water2").gameObject;
    }
     
    function Update () {
       altura_jogador = playerObject.transform.position.y;
   
    if (altura_jogador < waterLevel){
    //Daylight Water2 is my Day Light name!
   DaylightWater.SetActive(true);
    }else{
    DaylightWater .SetActive(false);
    }
    }

You can also insert Debug.Log(“hello!”) lines here and there to check which part of your script work.