- Home /
Tapping doesn't work when played on iPhone
There spawns enemies in my game, which u have to tap to kill. The problem is, when i test my game on my iphone by exporting it through xcode, i can't tap them. When i click on them with my mouse, on my computer it works, and when i use the unity remote app, i can tap them. But not when i export it to my iPhone. Everything else works in my game, but i cant tap the enemies.
The script i use for is this:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TouchInput : MonoBehaviour {
public LayerMask touchInputMask;
private List<GameObject> touchList = new List<GameObject>();
private GameObject[] touchesOld;
private RaycastHit hit;
// Update is called once per frame
void Update () {
#if UNITY_EDITOR
if(Input.GetMouseButton(0) || Input.GetMouseButtonDown(0) || Input.GetMouseButtonUp(0)) {
touchesOld = new GameObject[touchList.Count];
touchList.CopyTo(touchesOld);
touchList.Clear();
Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,out hit,touchInputMask)) {
GameObject recipient = hit.transform.gameObject;
touchList.Add(recipient);
if(Input.GetMouseButtonDown(0)) {
recipient.SendMessage("OnTouchDown", hit.point, SendMessageOptions.DontRequireReceiver);
} if(Input.GetMouseButtonDown(0)) {
recipient.SendMessage("OnTouchUp", hit.point, SendMessageOptions.DontRequireReceiver);
} if(Input.GetMouseButtonUp(0)) {
recipient.SendMessage("OnTouchStay", hit.point, SendMessageOptions.DontRequireReceiver);
}
}
foreach (GameObject g in touchesOld) {
if(!touchList.Contains(g)) {
g.SendMessage("OnTouchExit", hit.point, SendMessageOptions.DontRequireReceiver);
}
}
}
#endif
}
}
And on my enemy i have a script saying:
void OnTouchDown () {
if(GameObject.Find("spawn_Controller").GetComponent<Spawn>().gameOver == false) {
health = health -1;
Instantiate(blood,gameObject.transform.position, Quaternion.identity);
audio.PlayOneShot(gunShot, 0.7F);
audio.PlayOneShot(bloodSplat, 0.7F);
}
}
Can someone tell what is wrong?
Answer by LK84 · Jan 05, 2017 at 04:48 PM
You are using the UNITY_EDITOR directive, so of course all the code between #if UNITY_EDITOR
and #endif
won't be executed on your IPHONE. You basically have an empty Update() loop on your IPHONE.
Is it possible just to remove the UNITY_EDITOR and END IF and it wont affect anything?
You should read a little bit about preprocessor directives. They basically change the text of the source code before compilation and the result is a new source code without the code within these directives (in case if returns false). So if you use #if UNITY_EDITOR
and #endif
the code between these statements is not visible except from a program ran in the Unity Editor.
You can either remove these statements or write your own implementation for IOS, using the platform directive UNITY_IOS
. The same accounts for all other platforms to, of course. Have a look here: https://docs.unity3d.com/$$anonymous$$anual/PlatformDependentCompilation.html
To answer your question. Removing the directives will lead to having the same code compiled on all platforms. If this is what you want, then it is safe.