There is a match3 toy. For the mouse, the code was (the script hangs on the cube, the cube object is the same for everyone, only the color is changed))

void OnMouseDown() { //само действие } 

Works. I tried for the touchpad to do so

 public void fortouch() { if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) { // само действие }} 

fortouch itself is hung in Update. When we start the game, by pressing the screen, a large pile of dice is immediately allocated, although in Array there is only room for two.

The question is actually such - how to make the same touches on the touchscreen as if it were a mouse click? The resources for all the cubes are the same, they differ only in colors, maybe this is a snag for the failure of my code for the wheelbarrow? When there were no heaps of identical objects on the scene, it seemed to work fine. But the mouse does not interfere?

    1 answer 1

    Your script just checks that there is at least one touch and it is in the beginning phase of the touch. If you want to catch an event on objects, then ideally you need to go through all the touches, shoot a ray from the observation point through the tangency point and check which object it hits through physics. Here I found an example in the internet, we hang it on any object and it will check and call OnMouseDown on all objects where Touch was. Just keep in mind that physics is used, so a collider must be present on all objects that must be present.

     void Update() { foreach(Touch t in Input.Touches) { if(t.phase == TouchPhase.Began) { Vector3 point = new Vector3(t.position.x, t.position.y, 0); Ray ray = Camera.main.ViewportPointToRay(point); RaycastHit hit; if(Physics.Raycast(ray, out hit)) { hit.collider.SendMessage("OnMouseDown", null, SendMessageOptions.DontRequireReceiver); } } } }