- Home /
Detect Touch on GUI.Button in Script?
I'm trying to tell the game that if the Character Controller is grounded, and the GUI.Button (that has a running icon texture within it) is pressed, the walking speed of the character will become running speed.
Here is the portion of the script:
function SetSpeed()
{
var speed = walkSpeed;
if ( chCtrl.isGrounded && GUI.Button(Rect(1050, 350, 120, 100),btnTexture) )
{
speed = runSpeed;
}
chMotor.movement.maxForwardSpeed = speed;
}
In the line "if ( chCtrl.isGrounded && GUI.Button(Rect(1050, 350, 120, 100),btnTexture) )" of course "GUI.Button(Rect(1050, 350, 120, 100),btnTexture)" doesn't work at all. I'm just trying to show what I am trying to do.
Basically, have the script say: "... && GUI.Button is pressed". Any help would be greatly appreciated! :)
It's not an issue about pressing the button, it's an issue of how to word it on javascript.
if ( chCtrl.isGrounded && GUI.Button(Rect(1050, 350, 120, 100),btnTexture) )
{
speed = runSpeed;
}
When I play the game, it says:
"NullReferenceException: Object reference not set to an instance of an object UnityEngine.GUI.Button (Rect position, UnityEngine.Texture image) (at C:/BuildAgent/work/cac08d8a5e25d4cb/Runtime/ExportGenerated/Editor/GUI.cs:374) PlayerRunAndFootsteps.SetSpeed () (at Assets/_Scripts/PlayerRunAndFootsteps.js:59) PlayerRunAndFootsteps.Update () (at Assets/_Scripts/PlayerRunAndFootsteps.js:42)"
in the console. I need the walking speed to be kept at 6, and if the GUI.button is ever pressed, the speed would increase to 12, which is the running speed. I don't know how to script "if the button is pressed".
You should only make GUI calls inside OnGUI() Is SetSpeed() called from inside OnGUI()? If so, are you sure your btnTexture is not null?
Answer by Bunny83 · Oct 20, 2013 at 05:19 AM
First of all hopefully you know that you can use GUI stuff only in OnGUI. It's fine to use helper functions like your SetSpeed function but it needs to be called from OnGUI or the button won't be displayed and can't react to any event. For more information on how the GUI system works see GUI Basics or if you want to understand what happens inside the GUI controls and how the system works in detail, see my GUI crash course.
Next thing is GUI.Button returns true only once at the moment it is clicked (pressed down and released like a normal button). What you might want to use in your case is GUI.RepeatButton which returns true every frame as long as it is pressed down.
Last thing is that the GUI system and it's event system does not support multitouch. On mobile it simply emulates a single mouse by taking the average position of all touches.
If you need multitouch support you have to do your own touch handling with the input class.
This is a RepeatButton that works with multitouch:
// UnityScript
public class RepeatButtonState
{
static var hash = "RepeatButtonState".GetHashCode();
var fingerId = -1;
}
function RepeatButton(position : Rect, content : GUIContent, style : GUIStyle) : boolean
{
#if UNITY_IPHONE || UNITY_ANDROID
var controlID = GUIUtility.GetControlID(RepeatButtonState.hash, FocusType.Native, position);
var state = GUIUtility.GetStateObject(RepeatButtonState,controlID) as RepeatButtonState;
var down = false;
for (var touch : Touch in Input.touches)
{
var pos = touch.position;
pos.y = Screen.height - pos.y;
if (position.Contains(pos))
{
if (touch.phase == TouchPhase.Began)
state.fingerId = touch.fingerId;
if (touch.fingerId == state.fingerId)
down = true;
}
if (touch.phase == TouchPhase.Ended ||touch.phase == TouchPhase.Canceled)
if (touch.fingerId == state.fingerId)
state.fingerId = -1;
}
if (Event.current.type == EventType.Repaint)
{
style.Draw(position, content, down, down, false, false);
}
return down;
#else
return GUI.RepeatButton(position, content, style);
#endif
}
I've just written it :D but it's tested on Android