GUI Bug? (409687)

This is my main menu script:

var dampening = 0.01;

private var windowRect = Rect ((Screen.width/2) - 100, (Screen.height/2) - 100, 200, 200);
private var openWindow : int = 0;
private var highscoreUrl = "http://prometheusgame.x10hosting.com/highscores.php";
private var highscores : String = "Loading Scores...";

function Start() {
	getScores();
}

function Update() {
	transform.Translate(0, Mathf.Cos(Time.time) * dampening, 0);
	AudioListener.volume = PlayerPrefs.GetFloat("Volume");
}

function OnGUI () {
	if(!PlayerPrefs.HasKey("Username")) {
		var playerName : String;
		GUILayout.BeginArea(windowRect);
		GUILayout.Label("Please Provide a Username for the Leaderboard:");
		playerName = GUILayout.TextField(playerName, 15);
		if(GUILayout.Button("Submit")) {
			PlayerPrefs.SetString("Username", playerName);
		}
		GUILayout.EndArea();
	}
	else {
    	switch (openWindow){
    	case 0:
    		Menu();
    		break;
    	case 1:
    		Highscores();
    		break;
    	case 2:
    		Settings();
    		break;
    	}
	}
}

function Menu() {
	GUILayout.BeginArea(windowRect);
	if(GUILayout.Button("Single Player")) {
		Application.LoadLevel (1);
	}
	if(GUILayout.Button("View Highscores")) {
		openWindow = 1;
	}
	if(GUILayout.Button("Settings")) {
		openWindow = 2;
	}
	if(GUILayout.Button("Quit")) {
		Application.Quit();
	}
	GUILayout.EndArea();
}

function Settings() {
	if(!PlayerPrefs.HasKey("Propulsion Method")) {
		PlayerPrefs.SetString("Propulsion Method", "Force");
	}
	GUILayout.BeginArea(windowRect);
	GUILayout.Label("Propulsion Mode");
	if(PlayerPrefs.GetString("Propulsion Method") == "Velocity") {
		if(GUILayout.Button ("Force Mode")) {
			PlayerPrefs.SetString("Propulsion Method", "Force");
		}
	}
	if(PlayerPrefs.GetString("Propulsion Method") == "Force") {
		if(GUILayout.Button ("Velocity Mode")) {
			PlayerPrefs.SetString("Propulsion Method", "Velocity");
		}
	}
	if(!PlayerPrefs.HasKey("Volume")) {
		PlayerPrefs.SetFloat("Volume", 1.0);
	}
	GUILayout.Label("Game Volume");
	var volume : float = GUILayout.HorizontalSlider(PlayerPrefs.GetFloat("Volume"), 0.0, 1.0);
	PlayerPrefs.SetFloat("Volume", volume);
	GUILayout.Label("Username:");
	playerName = PlayerPrefs.GetString("Username");
	playerName = GUILayout.TextField(playerName, 15);
	PlayerPrefs.SetString("Username", playerName);
	if(GUILayout.Button("Return to Menu")) {
		openWindow = 0;
	}
	GUILayout.EndArea();
}

function Highscores() {
	GUILayout.BeginArea(windowRect);
	GUILayout.Label(highscores);
	if(GUILayout.Button("Return to Menu")) {
		openWindow = 0;
	}
	GUILayout.EndArea();
}

function getScores() {
    var hs_get : WWW = new WWW(highscoreUrl);
    yield hs_get;
    if(hs_get.error) {
        Debug.Log(hs_get.error);
        highscores = "There was an error retrieving the scores.";
    }
    else {
    	highscores = hs_get.data;
    }
}

for some reason, this only works half the time in the editor, all the time in a standalone, and never in a webplayer. i’ve been wracking my brain on this for days. what have i done wrong?

i figured out part of the problem:

sometimes the GUI renders at

Rect((Screen.width/2) - 100, (Screen.height/2) - 100, 200, 200)

and sometimes it renders at

Rect(Screen.width - 100, Screen.height - 100, 200, 200)

why is this?[/code]

First thing to try is to initialise windowRect within the Start function rather than on the declaration line.

Hey ozymandias, I dont mean to hijack your thread, but I have a similar issue.

@ andeeee -
I am currently making a GUI interface, and up until now, Screen.width and Screen.height have worked fine, without setting variables in the Start() function.

Now im running into a wall where I have to declare the variables, then set them in the Start() function.

Why is this? Why has this worked flawlessly before? Am I doing something incorrect, or is this a bug? Thanks for your time.

it likely worked because you were either lucky or didn’t have it in the scene but added / instantiated it later or or something alike.

generally the rule is: any access to objects outside this one should be done in start because there is no guarantee for anything to be ready if you try it earlier (in awake or the declaration)

Awesome, thanks for the clarification dreamora. This is good to know, and I since updated my scripts accordingly and all is working sexy now. :lol:

I guess its one of those things that comes from walking the learning path of Unity.