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 Qasem2014 · Jan 04, 2015 at 10:40 AM · uitouchscreen

Problem with touch and ui button

HI

i have this code in a empty game object :

 function Update()
 {
     if (Input.touchCount > 0)
     {
         var touch = Input.GetTouch(0);
         if(touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary)
         {
             CursorRay = Camera.main.ScreenPointToRay(Input.GetTouch(0).position );
             if(Physics.Raycast( CursorRay, Hit, 1000.0f ) )
             {
                 if(Hit.transform.tag == "Objects")
                 { TargetAhead = GameObject.Find(Hit.transform.name); }
                 else { TargetAhead = GameObject.Find("Ground"); }
             }
         }
     }
 }

it get a ray to touch position in 3d space and set "TargetAhead" variable to touched game object . i have a button (unity 4.6) and when it clicked this function will call :

 function Shoot()
 { print(TargetAhead.name); }

but there is a problem . i can't click it when i touching the screen ! how can i make it clickable when i touching the screen ?

<< also i limit touch position to every where in screen but button >>

Comment
Add comment · Show 1
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 fafase · Feb 21, 2015 at 08:38 AM 0
Share

To avoid touch when on a GUI item you use:

http://docs.unity3d.com/460/Documentation/ScriptReference/EventSystems.EventSystem.IsPointerOverGameObject.html

5 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by cr_carvalho · Mar 19, 2015 at 10:19 AM

Try Disable Standalone Input Module (Script) from EventSystem

alt text


eventsystem.png (18.1 kB)
Comment
Add comment · Show 2 · 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
avatar image Qasem2014 · Mar 21, 2015 at 03:36 PM 0
Share

Very GOOOOOOOD :)

Thank You $$anonymous$$r :D

avatar image dottyfdsf · May 13, 2015 at 08:35 PM 1
Share

Thanks! But be careful - it doesn't work on Unity Remote, only when you Build a game. ;)

avatar image
0

Answer by CUBIX · Jan 05, 2015 at 09:11 PM

Maybe this? Maybe this?


sol.png (32.1 kB)
Comment
Add comment · Show 1 · 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
avatar image Qasem2014 · Jan 06, 2015 at 10:26 AM 0
Share

Still not work on touch ://

avatar image
0

Answer by Mmmpies · Feb 16, 2015 at 12:11 PM

Make that function public. Select the button in the hierarchy. Look in the inspector for on click.

Make sure the script with the, now public, Shoot function is on the button. Then click + in on click and drag the button with the script on it onto the slot that appears.

From the dropdown select YourScriptName -> Shoot.

That should work for mouse or finger clicks.

Comment
Add comment · Show 5 · 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
avatar image Qasem2014 · Feb 17, 2015 at 05:48 PM 0
Share

i make shoot function public but nothing change . shoot function also assigned to onclick() eventt

avatar image Mmmpies · Feb 18, 2015 at 02:44 PM 0
Share

Sorry missed this update. Can you put a simple code in the shoot function so it changes the buttons color. Test that on your computer then retest on your touch device.

If it still fails take off the limit on touches you mentioned and test again.

EDIT

I meant this comment:

<< also i limit touch position to every where in screen but button >>

I don't think you said how you did that

avatar image Qasem2014 · Feb 18, 2015 at 05:44 PM 0
Share

limit ? you mean this codes :

 if (Input.touchCount > 0)
 {
    var touch = Input.GetTouch(0);

well i remove if but it gets error if i remove zero from GetTouch(0) !!!!

its syntax ! right ?

maybe i don't understand you ! so explain more :)

avatar image Mmmpies · Mar 03, 2015 at 08:50 PM 1
Share

Sorry @Qasem2014 I missed your update, get too many e-mails about Unity!

Let's take this back down to as simple as we can, create a new project and add a canvas with a Text and a Button on it.

Write a script with a public function to display "Button Pressed" in the Text. Drag that script on the Button and in the OnClick for that button click + drag the Button (with your script on it) to the slot that appears and from the dropdown select YourScriptName -> YourPublicFunction.

Test that on your touch device, touch does work.

avatar image LongChau · Jan 07, 2016 at 10:00 PM 0
Share

You are saint!

avatar image
0

Answer by reizibarrientos · Jul 02, 2017 at 09:23 AM

Hi I think you need to expand or scale your buttons! And hey also you need to enable your first module active inside the eventsystem!

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
avatar image
0

Answer by pedrampk · Jul 13, 2018 at 02:16 PM

in a project, my UI buttons worked in Editor but not worked in Android device. my problem was solved after searching on "problems with IsPointerOverGameObject()" function. the bellow page helped me to solve this problem. I hope it is usefull for you.

https://answers.unity.com/questions/1115464/ispointerovergameobject-not-working-with-touch-inp.html

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

31 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 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

Problem with touch and ui button 1 Answer

Advanced Unity: Custom touch input origin 1 Answer

Low screen sensitivity on Mobile 3 Answers

How to prevent touch passing through UI ? 0 Answers

Make a Triangle instead of a "Rect" 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