Global Variable Error

I am trying to modify the default character motor script to add swimming capabilities. I have a first person controller and an island scene. Since I have the free version of Unity, I have the water as being a square plane at y = 1010 with a script to create waves. The script that I attached to the main camera was something like this:

static var isUnderwater : boolean;
    
function Update(){

    var underwater = 1010;

    if(transform.position.y < underwater){
        isUnderwater = true;
    }
    else{
        isUnderwater = false;
    }

    if(isUnderwater == false){
	    audio.Play();
    }
}

I also had the character motor script attached to the first person controller. Obviously that is too long to post here but in the private function UpdateFunction() on line 185, I wanted to put

if(UnderwaterScript.isUnderwater == true){
    canControl = false;
}

When I try to compile the script it throws an error that says “Unknown Identifier: ‘UnderwaterScript’.” I know what this means (It doesn’t know what UnderwaterScript is.), but I don’t know how to fix it. I’ve tried all different capitalization of UnderwaterScript, and i’ve tried changing the isUnderwater variable to var, static var, and public var. All the other posts that I looked at simply say that you need to use the name of the script that the global variable is located in followed by a dot, followed by the name of the variable. I did that. Could someone please help me?

It’s funny, usually the people have problems because they mix different languages so they need to place the different files in different compiling groups. In your case that’s actually the problem. The character motor script is usually located in the Standard Assets folder and therefore compiled before everything else. Your own custom script is propably somewhere in your assets folder and compiled after the character motor script. That means it isn’t available to the character motor script.

So you should either move your own script into the Standard Assets folder or move the character motor script out of that folder so both files are in the same group.

For more information see the compilation order page