- Home /
GUI Button Disappearing
I have four directional buttons in a scene to control a mars rover. When one button is clicked, the other remaining buttons disappear while the mouse is down. Has anyone exerienced a similiar problem? I'm probably overlooking something obvious, however any input is appreciated.
Here is my code for my main GUI (js) :
var motionDir: Vector3; // var ctrl: CharacterController;
var rover : GameObject;
private var GetKeyDown : boolean; private var buttonOn : boolean; private var windowOn : boolean;
var style : GUIStyle;
// BG/Template: var mainImage: Texture2D; var mainImageOffset = Vector2(0, 0);
//GUI active elements: var dPadOutline : Texture2D; var dPadLeft : Texture2D; var dPadRight : Texture2D; var dPadUp : Texture2D; var dPadDown : Texture2D;
var dPad : Texture2D; var gak : Texture2D; var histogram : Texture2D; var timerBG : Texture2D;
var position : Rect; var nativeVerticalResolution = 768;
//var btn1 : GUIStyle;
var cameraIris : MovieTexture;
var Active : boolean = true;
function OnGUI() {
if (!rover.animation.IsPlaying ("startUp") && !rover.animation.IsPlaying ("armDrillAnimation"))
{
if(GUI.RepeatButton (Rect(870,383,43,43), dPadUp, "button"))
//if(GUI.RepeatButton (Rect(870,468,43,43), dPadUp, "label"))// || (Event.current.type == EventType.KeyUp && Event.current.keyCode == KeyCode.Alpha1))
{
Drive();
return EventType.MouseDown;
}
if( GUI.RepeatButton(Rect(870,524,43,43), dPadDown, "button"))
{
DriveReverse();
return EventType.MouseDown;
}
if( GUI.RepeatButton(Rect(794,455,43,43), dPadLeft, "button")) // || (Event.current.type == EventType.KeyUp && Event.current.keyCode == KeyCode.Alpha1))
{
TurnLeft();
return EventType.MouseDown;
}
if( GUI.RepeatButton( Rect(932,455,43,43), dPadRight, "button"))
{
TurnRight();
return EventType.MouseDown;
}
else if (!rover.animation.IsPlaying("drive") && rover.animation.IsPlaying("wheelTurnOut"))
{
rover.animation.CrossFade ("wheelTurnBack", 1);
return EventType.MouseUp;
}
else
{
rover.animation.CrossFade ("staticPose");
return EventType.MouseUp;
}
//Draw bottom GUI section (main) DrawImageBottomAligned( mainImageOffset, mainImage); // main image.
}
/// Detects keys pressed and prints their keycode var e : Event = Event.current;
if (e.isKey)
{
Debug.Log("Detected key code: " + e.keyCode);
}
}
function Drive () { rover.animation.Blend ("drive", 0.3); rover.transform.Translate(Vector3.forward); Debug.Log("Available id: " + GUIUtility.GetControlID(FocusType.Passive)); print("DRIVE!!!"); }
function TurnLeft () { rover.animation.CrossFade ("wheelTurnOut", 0.05); rover.animation.Blend ("rotateLeft", 0.4); rover.transform.Rotate(Vector3.down); Debug.Log("Clicked the d-pad Left"); }
function TurnRight () { rover.animation.CrossFade ("wheelTurnOut", 0.05); rover.animation.Blend ("rotateRight", 0.4); rover.transform.Rotate(Vector3.up); Debug.Log("Clicked the d-pad Right");
}
function DriveReverse() { rover.animation.Blend ("driveInReverse", 0.3); rover.transform.Translate(Vector3.forward*-1); Debug.Log("Clicked the d-pad Down"); }
// Handles positioning of elements on screen function DrawImageBottomAligned (pos : Vector2, image : Texture2D) { GUI.Label(Rect (pos.x, nativeVerticalResolution - image.height - pos.y, image.width, image.height), image); }
function DrawLabelBottomAligned (pos : Vector2, text : String) { GUI.Label(Rect (pos.x, nativeVerticalResolution - pos.y, 100, 100), text); }
function DrawImageBottomRightAligned (pos : Vector2, image : Texture2D) { var scaledResolutionWidth = nativeVerticalResolution / Screen.height * Screen.width; GUI.Label(Rect (scaledResolutionWidth - pos.x - image.width, nativeVerticalResolution - image.height - pos.y, image.width, image.height), image); }
function DrawLabelBottomRightAligned (pos : Vector2, text : String) { var scaledResolutionWidth = nativeVerticalResolution / Screen.height * Screen.width; GUI.Label(Rect (scaledResolutionWidth - pos.x, nativeVerticalResolution - pos.y, 100, 100), text); }
Answer by _MGB_ · Feb 21, 2011 at 08:45 PM
You're returning from the OnGUI function if a button event is handled - therefore the other button code doesn't get run :)
Jeez, that makes total since. So to maintain functionality and appearance I'm adding this under each respective button: GUI.Button(Rect0,0,0,0,), dPadUp, "button"); Is there a better approach? Thanks for your help!
I'm not sure what you mean there, but just removing the return statements should sort things out.
Removing the return statements fixes the problem of the buttons disappearing, however the RepeatButton functionality does not seem to stay intact doing so.
Does this shed any light on your problem? http://forum.unity3d.com/threads/19527-RepeatButton-true-false
That solves the button disappearing issue, my drive animation gets lost when using Update() and OnGUI() with the same method mentioned in the link you posted. I should be able to link up the animations which are triggered along with the button press, I'll keep working on it. Thanks again! :)
Your answer
Follow this Question
Related Questions
Javascript GUI.Button help, Error BCE0077 1 Answer
NGUI Repeat button In JavaScript problem 1 Answer
GUI.Button not appearing when called, no errors 1 Answer
GUIText Problem With MENU 1 Answer
Gui Button Solid 2 Answers