Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Halo500 · Oct 20, 2013 at 01:22 AM · guitexturegui-button

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! :)

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Halo500 · Oct 20, 2013 at 03:37 AM 0
Share

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".

avatar image robertbu · Oct 20, 2013 at 03:47 AM 0
Share

You should only make GUI calls inside OnGUI() Is SetSpeed() called from inside OnGUI()? If so, are you sure your btnTexture is not null?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Battery Collect Collision Problem 2 Answers

Situational GUI popup question. 3 Answers

GUI.Button not showing up 1 Answer

problem using input key inside a trigger for calling an image (gui texture) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges