Marking a variable as static breaks the script?

Hello,

this is my first post to the community, until now, I just read a lot here, but for my current problem I don’t really find an answer. It’s a very simple problem, but I just cant find a solution.

I have 2 GUI Textures. The Second one should become visible on MouseOver, but only if the first one is already visible.

Here’s the code of the first texture which makes it visible when the user presses Escape

var showinapp=0;

function Update () {

if(Input.GetKeyUp(KeyCode.Escape)&&showinapp==0){

gameObject.GetComponent(GUITexture).enabled=true;
showinapp=1;

}

}

And here’s the code I tried for the second Texture

function Update () {

guiTexture.color.a = loAlpha;


if(Show_InApp.showinapp==1 && guiTexture.HitTest(Input.mousePosition)){

loAlpha=1;

}}

Here’s the problem: Making “showinapp” static breaks the code of the first script. It doesn’t produce any compiling errors, but the Texture doesn’t get active anymore if the user presses Escape.

Does anyone have an idea how to solve this?

Making a variable static doesn’t make it “global”, it means there can be only one instance of it in a class. A public (non-static) variable is global.

Your First Script that reveals the Gui Texture on pressing escape would work perfectly with the script below:

#pragma strict

var showinapp = false;

function Update () 
{
	if(Input.GetKeyUp(KeyCode.Escape)&&showinapp==false)
	{
		gameObject.GetComponent(GUITexture).enabled=true;
		showinapp=true;
	}
	else if(Input.GetKeyUp(KeyCode.Escape)&&showinapp==true)
	{
		gameObject.GetComponent(GUITexture).enabled=false;
		showinapp=false;
	}
}

Make sure to disable the Gui Texture before playing in the editor.

You don’t want to make the variable static. You will want to reference the first script that contains “showinapp” from the 2nd script:

public var ScriptName;

Then you will be able to access the variable “showinapp”.

Before clicking play drag the GUITexture that has the first script attached into the inspector of the GuiTexture that holds the second script. Otherwise you get a null reference exception.