- Home /
GUI button inconsistent
I have implemented several GUI buttons to control scene loading in my game. My problem is the buttons on sceneMenu execute everytime. On sceneLoose, the one button executes almost every time. On sceneWin the next level button has to be clicked two or three times about every 4 calls to this scene. The menu button has to be spam clicked to even see the console print and never loads the menu scene.
For testing purposes I included a print in the controlling if statement to see if unity was seeing the button press. Indeed this print statement is being hit eventually on all but one button and when it prints to the console the level is loaded immediately. The other button is just never seen.
Also, the graphics of the GUI button are showing its being clicked.
Stripped down code follows:
//SceneWin
(Alot of Vars here)
function OnGUI(){
(Alot of Lables here)
var nextLevelButton = GUI.Button(Rect(button1X,button1Y,button1W,button1H),"Next Level");
var menuReturn = GUI.Button(Rect(button2X,button2Y,button2W,button2H),"Main Menu");
if(nextLevelButton){
print("Level Button Pressed, Loading");
Application.LoadLevel("sceneLevel_1");
}
if(menuReturn){
print("menu return pressed");
Application.LoadLevel("sceneMenu");
}
}
Next Scene
//sceneLoose
//(virtually same vars as sceneWin)
function OnGUI(){
//(virtually same lables as sceneWin)
var menuReturn = GUI.Button(Rect(button2X,button2Y,button2W,button2H),"Main Menu");
if(menuReturn){
player.PlayerReset();
Application.LoadLevel("sceneMenu");
}
}
Menu scene
//sceneMenu
//(same vars, mostly, as other two scenes)
function OnGUI(){
var player = GameObject.FindWithTag("Player").GetComponent(scPlayer); //get player
var startButton = GUI.Button(Rect(button1X,button1Y,button1W,button1H),"S T A R T");
GUI.Label(Rect(label1X,label1Y,label1W,label1H), "Level Select");
var levelSelectUp = GUI.Button(Rect(button2X,button2Y,button2W,button2H),"UP");
GUI.Label(Rect(label2X,label2Y,label2W,label2H), "" + selectPlayerLevel);
var levelSelectDown = GUI.Button(Rect(button3X,button3Y,button3W,button3H), "Down");
if(startButton){
Application.LoadLevel("sceneLevel_1");
GameObject.FindWithTag("Player").GetComponent(scPlayer).SetPlayerState("levelEnd",false);
print("start game");
}
if(levelSelectUp){
selectPlayerLevel = selectPlayerLevel + 1;
player.playerLevel = selectPlayerLevel;
}
if(levelSelectDown && selectPlayerLevel >1){
selectPlayerLevel = selectPlayerLevel -1;
player.playerLevel = selectPlayerLevel;
}
}
What about?:
if(GUI.Button(Rect(button1X,button1Y,button1W,button1H),"Next Level")){
//Load level here
}
Well, gave it a go and it didn't change the behavior. However I took a closer look at my log file. In the past it was just showing something along the lines of assets loaded/unloaded and I never saw my print statements in it. I just assumed that it had something to do with my game objects being destroyed and spawned in the game and after the next scene there was nothing to display in the log file because I was not hitting the print statement.
On close inspection (I have to scroll up some 100 paragraphs) I found the print statement. What follows is the log file from where the button is pressed and the last entry just keeps repeating and log file keeps growing.
Blockquote menu return pressed UnityEngine.Debug:Internal_Log(Int32, String, Object) UnityEngine.Debug:Log(Object) UnityEngine.$$anonymous$$onoBehaviour:print(Object) (at >C:\BuildAgent\work\812c4f5049264fad\Runtime\ExportGenerated\Edi>tor\UnityEngine$$anonymous$$onoBehaviour.cs:63) scWin:OnGUI() (at Assets\scWin.js:114)
(Filename: Assets/scWin.js Line: 114)
Unloading 1 Unused Serialized files (Serialized files now >loaded: 0 / Dirty serialized files: 0)
Unloading 4 unused Assets to reduce memory usage. Loaded >Objects now: 561. Operation took 22.295954 ms. System memory in use: 20.2 $$anonymous$$B. Unloading 0 Unused Serialized files (Serialized files now >loaded: 0 / Dirty serialized files: 0)
Unloading 0 unused Assets to reduce memory usage. Loaded >Objects now: 516. Operation took 6.055507 ms. System memory in use: 20.1 $$anonymous$$B. Unloading 1 Unused Serialized files (Serialized files now >loaded: 0 / Dirty serialized files: 0)
Unloading 4 unused Assets to reduce memory usage. Loaded >Objects now: 561. Operation took 11.681388 ms. System memory in use: 20.2 $$anonymous$$B. Unloading 1 Unused Serialized files (Serialized files now >loaded: 0 / Dirty serialized files: 0)
Unloading 4 unused Assets to reduce memory usage. Loaded >Objects now: 553. Operation took 15.612651 ms. System memory in use: 20.2 $$anonymous$$B. Unloading 1 Unused Serialized files (Serialized files now >loaded: 0 / Dirty serialized files: 0)
Just another thought. I do have a singelton running (player). But its just :
function Update () {
if(levelEnd == false){
ShotCheck();
}
else{
EndLevel();
}
}
and levelEnd is toggling as it should so its set to true during this GUI menu mess. That being said, the entries in the log may just be co$$anonymous$$g from it parsing that update thread.
I wrote a quick project that uses 2 scenes each scene calling the other. In one of the scenes I placed a print function in update and observed the logs. Script and output follows. Debug Log
nloading 1 Unused Serialized files (Serialized files now loaded: 0 / Dirty serialized files: 0)
Unloading 4 unused Assets to reduce memory usage. Loaded Objects now: 515. Operation took 11.001113 ms.
System memory in use: 11.9 $$anonymous$$B.
Unloading 0 Unused Serialized files (Serialized files now loaded: 0 / Dirty serialized files: 0)
Unloading 0 unused Assets to reduce memory usage. Loaded Objects now: 479. Operation took 5.471575 ms.
System memory in use: 11.8 $$anonymous$$B.
scene 2 start
UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
UnityEngine.$$anonymous$$onoBehaviour:print(Object) (at C:\BuildAgent\work\812c4f5049264fad\Runtime\ExportGenerated\Editor\UnityEngine$$anonymous$$onoBehaviour.cs:63)
scene2:Start() (at Assets\scene2.js:7)
//scene 1
function OnGUI(){
print("GUI Scene 1");
Application.LoadLevel("scene2");
}
//scene 2
function Start(){
print ("scene 2 start");
}
function Update(){
print("scene 2 update");
}
function OnGUI(){
print("GUI Scene 2");
Application.LoadLevel("scene1");
}
The first thing I notice is that the same load/unload lines appear but they are getting cut off as the next scene loads. This leads me to believe that the Application.LoadLevel is never called despite the code being correct as far as I can tell. I'd call this a bug but not sure how to even prove its a bug.
Im still getting this behavior with no clue why... just bumping it back up.