• 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 /
This question was closed Feb 01, 2021 at 10:29 AM by Bunny83 for the following reason:

Problem is outdated. OP hasn't been on for more than 7 years. Question is closed to avoid future bumping. Comments will be locked as well.

avatar image
0
Question by dumbquestions · Mar 07, 2014 at 04:12 AM · android2dtouch

TouchCount always > 0 on Android

I'm working with Unity/Android for the first time. I have several objects on the screen all with the same script on each. I want to be able to independently drag each object around. T$$anonymous$$s works fine when I test it in the game tab, but when I actually build on Android it doesn't work correctly. Instead, after touc$$anonymous$$ng an object once, anytime I touch the screen anywhere else it instantly jumps there. Not just when I drag. Could anyone tell me what i'm doing wrong (and maybe a few other tips? I know the way i'm doing t$$anonymous$$s isn't the best...)

 using UnityEngine;
 using System.Collections;
 
 
 public class handleTouch : MonoBehaviour {
 
     // Use t$$anonymous$$s for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
 
     public bool dragging = false;
     void Update()
     {
         var aTouch = Input.GetMouseButton(0);
         var touchPos3D = Input.mousePosition;
         var firstTouch = false;
 
         if (Application.platform != RuntimePlatform.IPhonePlayer && Application.platform != RuntimePlatform.Android)
             
         {
             if (Input.GetMouseButtonDown(0)){
                 firstTouch = true;
             }
         } 
         else 
         {
             aTouch = (Input.touchCount > 0);
             touchPos3D = Input.touches[0].position;
 
             if (Input.touches[0].phase != TouchPhase.Moved){
                 firstTouch = true;
             }
         }
 
         if (aTouch)
         {
             Vector3 wp = Camera.main.ScreenToWorldPoint(touchPos3D);
             Vector2 touchPos2D = new Vector2(wp.x, wp.y);
 
             
             if (t$$anonymous$$s.collider2D == Physics2D.OverlapPoint(touchPos2D) && firstTouch == true)
             {
                 dragging = true;
             }
             else
             {
                 if (dragging == true)
                 {
                     t$$anonymous$$s.transform.position = touchPos2D;
                 }
             }
         }
         else
         {
             dragging = false;
         }
     }
 }
 
Comment
Comments Locked · Show 3
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 shieldgenerator7 · Apr 17, 2020 at 09:31 PM 0
Share

Did you ever figure out a solution?

avatar image escapaid · Feb 01, 2021 at 05:18 AM 0
Share

I also have this same issue. Can anyone assist?

avatar image Bunny83 escapaid · Feb 01, 2021 at 10:28 AM 0
Share

I can almost guarantee that you do not have the same issue. If you have a question, please ask your own seperate question with your own actual code. This is a Q&A site and not a forum. Apart from that pretty much any forum or Q&A site on this planet do not want necro posting or thread / question hijacking.

1 Reply

  • Sort: 
avatar image
1

Answer by Bunny83 · Feb 01, 2021 at 10:22 AM

I know the question is an ancient question but since it got bumped, here's the answer:


The issue is most likely line 31 and 33 where you blindly try to access touch 0 from the touches array regardless of the touch count. T$$anonymous$$s will cause an index out of bounds exception and no code that follow that line would run if no touch is active. Therefore when the touch count is 0 you never reach down to your code where you reset your dragging variable back to false. So when a new touch occurs you are still "dragging".


I $$anonymous$$ghly doubt that the OP still needs t$$anonymous$$s information since he wasn't seen here on UA in the last 7 years. I also doubt that the answer will be helpful to any of the necro bumpers but it may help others who actually overlooked the fact that the touches array can be empty.

Comment
Comments Locked · 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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Raycast Touch Android not working JS 2 Answers

Can't get touch position in variable? 1 Answer

OnPointerExit Issue w/ Radial Menu on Android 1 Answer

Camera swipe AND gameObject touch (Android) 1 Answer

touch position to world position 2D 2 Answers


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