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 infinitypbr · Dec 28, 2012 at 11:08 PM · touchcontroller

Touch Event throwing XCode Error (not sure what it is - screenshot)

[PLEASE look at the 1st answer below -- I've gotten it narrowed down, but still need some assistance if possible.]

I'm using the information here ( http://wiki.unity3d.com/index.php?title=OnMouseDown ) to build a simple 2d-scroller control interface. (pressing on left, move left etc).

My code:

 var hit : RaycastHit;
     for (var i = 0; i < Input.touchCount; ++i) 
     {
         if (Input.GetTouch(i).phase == TouchPhase.Began) 
         {
             // Construct a ray from the current touch coordinates
             var ray = camera.ScreenPointToRay (Input.GetTouch(i).position);
             if (Physics.Raycast (ray,hit)) 
             {
                 Debug.Log("Hit " + hit.collider.gameObject.name);
             }
         }
     }

Upon running the game on the iPad, as soon as I press the screen, the game freezes and I get this error in Xcodealt text

Any ideas what this is, or is my code not proper?

screen shot 2012-12-28 at 3.05.52 pm.png (255.8 kB)
Comment
Add comment · Show 9
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 infinitypbr · Dec 28, 2012 at 11:24 PM 0
Share

Trying alternate methods, same error with this code:

 if ( iPhoneInput.touchCount > 0 ){
     for(var touch : iPhoneTouch in iPhoneInput.touches) { 
             Debug.Log(touch.position);
     }
 } 
avatar image Bunny83 · Dec 29, 2012 at 01:11 AM 0
Share

Are you sure that this is your only code that react to touch events?

$$anonymous$$eep in $$anonymous$$d that touches simulate mouse events for GUI and Input so make sure that's actually the source of your problem.

Btw the xcode snippet doesn't help much, this could be anywhere in your code.

avatar image infinitypbr · Dec 29, 2012 at 01:56 AM 0
Share

That is the only touch event in e script. It's the first touch event I'm working on. I have another project that works with touch using the click functions, but this requires multiple touches at once over a long period rather than individual clicks/touches.

avatar image infinitypbr · Dec 29, 2012 at 04:55 AM 0
Share

This code is pulled directly from the Unity docs [ Input.touches ], and gives the same error (inside Update):

 var fingerCount = 0;
 for (var touch : Touch in Input.touches) {
     if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
         fingerCount++;
 }
 if (fingerCount > 0)
     print ("User has " + fingerCount + " finger(s) touching the screen");


Obviously there's a reason this is happening, but for the life of me I can't figure it out. The game works fine without this code in it.

avatar image Seth-Bergman · Dec 29, 2012 at 05:00 AM 0
Share

there is nothing wrong with the code you are showing, the problem is elsewhere in your project, either in this or some other script.. what OTHER scripts do you have going in this project, or what else is in this one?

Show more comments

1 Reply

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

Answer by infinitypbr · Dec 29, 2012 at 07:43 AM

Some more information: I've gotten things to work, except i'm running into the problem again. Now it's only happening when I press Two Buttons w/ One Finger at once, and with the code below. I can press two buttons with two fingers just fine, but when I lay my thumb, for instance, over two buttons, the error occurs.

  • If I am not running xcode, the game works without the error. (Of course pressing two buttons only returns a "hit" on one of them **

Two questions:

1 What is the error that happens in xCode, and is there a way to ignore/stop it, since the game works on the iPad. I'd like to continue using the console in Xcode to read about other things going on in the game.

2 Is there a better way to accomplish this -- to read what buttons are being pressed and react? Is there a way to lay a thumb over two buttons and have them both be "hit"? (Kind of like NES pads when you pressed "B" to run fast and "A" to jump, but all the time holding down "B".)

 var hit2 : RaycastHit;
 for (var i = 0; i < Input.touchCount; ++i) 
     {
     print ("Touch " + i + " Position " + Input.GetTouch(i).position);
     var ray2 = mainCamera.ScreenPointToRay (Input.GetTouch(i).position);
     if (Physics.Raycast (ray2,hit2))
         print ("Hit w/ Touch:  " + hit2.collider.gameObject.name);
                 if (hit2.collider.gameObject.name == "Left Button")
                 {
                     pressingLeft = true;
                 }
              if (hit2.collider.gameObject.name == "Right Button")
                 {
                     pressingRight = true;
                 }
              if (hit2.collider.gameObject.name == "Up Right Button")
                 {
                     pressingRight = true;
                     pressingUp = true;
                 }
              if (hit2.collider.gameObject.name == "Up Button")
                 {
                     pressingUp = true;
                 }
              if (hit2.collider.gameObject.name == "Up Left Button")
                 {
                     pressingLeft = true;
                     pressingUp = true;
                 }
              if (hit2.collider.gameObject.name == "Fire Button")
                 {
                     print ("Hit Fire");
                     firing = true;
                 }
              if (hit2.collider.gameObject.name == "Jet Button")
                 {
                     print ("Hit Jet");
                     pressingUp = true;
                 }
     }
 
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 infinitypbr · Dec 30, 2012 at 12:25 AM 0
Share

I've discovered the problem. Can't explain why, but I fixed it.

if (Physics.Raycast (ray2,hit2)) print ("Hit w/ Touch: " + hit2.collider.gameObject.name); if (hit2.collider.gameObject.name == "Left Button") { pressingLeft = true; }

The code above (part of a longer snippet, way above), breaks when you touch anything that isn't an object that it listed in the if statements.

Adding brackets to include the if statements inside the first fixes it, as follows:

if (Physics.Raycast (ray2,hit2)) { print ("Hit w/ Touch: " + hit2.collider.gameObject.name); if (hit2.collider.gameObject.name == "Left Button") { pressingLeft = true; } }

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

10 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

Related Questions

Dual joystick Position always resets on playing 1 Answer

Unity 2D Touch Controll via on screen button for Mobile 0 Answers

Swipe menu, problem! 0 Answers

Move object with touch 3D with script 0 Answers

Swapping Horizontal Input for Mobile 0 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