- Home /
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 >>
To avoid touch when on a GUI item you use:
Answer by cr_carvalho · Mar 19, 2015 at 10:19 AM
Try Disable Standalone Input Module (Script) from EventSystem
Thanks! But be careful - it doesn't work on Unity Remote, only when you Build a game. ;)
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.
i make shoot function public but nothing change . shoot function also assigned to onclick() eventt
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
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 :)
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.
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!
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613175558im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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