Touch a Sprite

Hello,
I’m new with Unity (but I’m not new with coding), i came from Xcode and SpriteKit.
I have been building a 2D game with Unity, and i have a simple sprite (a jump icon) and a character. All i want is that if i touch the jump icon (the Sprite) something happen in the game. In C#, how can i say “If i touch that sprite with that name do something”. When i was using SpriteKit i was simply write:

If(node.name == “name”) {.do something } inside a touchesBegan method.

How can i do that in Unity?

Thanks.

You want to “touch” it with a player or as an input (touch screen) ? :?

If the sprite has a collider, you can use OnMouseDown:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    void OnMouseDown()
    {
        Application.LoadLevel("SomeLevel");
    }
}

I believe this does work for touch, but not multi-touch.

1 Like

Hi thanks,

Yes i want to touch it as an input touch (with finger).

i could find this code:

for (var i = 0; i < Input.touchCount; ++i) {
            if (Input.GetTouch(i).phase == TouchPhase.Began) {
                RaycastHit2D hitInfo = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(i).position), Vector2.zero);
                // RaycastHit2D can be either true or null, but has an implicit conversion to bool, so we can use it like this
                if(hitInfo)
                {
                    // Here you can check hitInfo to see which collider has been hit, and act appropriately
                    Debug.Log( hitInfo.transform.gameObject.name );


                }
            }
        }

It works but i’ve got a small problem: The jump button in the scene is child of the player, so when i touch the button it’s like touching the player… How can i make it recognise the button even if it is a player’s child?

Well, there’s probably a couple of ways to handle that. You can do a RaycastAll, and get multiple results, then check if any of the results were your button.

Another option would be to define a LayerMask, and use that LayerMask to only check for objects on that Layer.

So you can create a “UI” layer perhaps, set your button to that layer, then do this:

public LayerMask touchLayers; // set up in the inspector

public void function() {
    for(var i = 0; i < Input.touchCount; ++i) {
        if(Input.GetTouch(i).phase == TouchPhase.Began) {
            Vector3 position = Camera.main.ScreenToWorldPoint(Input.GetTouch(i).position);
            // pass in LayerMask
            RaycastHit2D hitInfo = Physics2D.Raycast(position, Vector2.zero, Mathf.Infinity, touchLayers);
            // RaycastHit2D can be either true or null, but has an implicit conversion to bool, so we can use it like this
            if(hitInfo) {
                // Here you can check hitInfo to see which collider has been hit, and act appropriately
                Debug.Log(hitInfo.transform.gameObject.name);
            }
        }
    }
}

Another way would be to use collider2D.OverlapPoint() to check that specific collider for the mouse position overlap.

Thanks,
Okay i did the layers and it works, but the problem is the same: The button is child of player, so in console appears “Player” and not the real name of the button. How can i fix that?

If the layers are set up correctly, you would never detect the “Player” gameObject. The “hitInfo” would have a reference to the child object only.