- Home /
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 Xcode
Any ideas what this is, or is my code not proper?
Trying alternate methods, same error with this code:
if ( iPhoneInput.touchCount > 0 ){
for(var touch : iPhoneTouch in iPhoneInput.touches) {
Debug.Log(touch.position);
}
}
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.
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.
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.
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?
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;
}
}
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
Follow this Question
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