Home > Net >  Unity 2d mobile button c# script is not working
Unity 2d mobile button c# script is not working

Time:11-16

I want to know how to do something on a touch. Please write any script example or what to do.

public Transform MoveThis;
public GameObject Here;

void OnClick() => MoveThis.transform.position = Here.transform.position;

I watched a lot of youtube videos and I tried a lot of scripts but it seems none worked for me.

CodePudding user response:

The Unity documentation is a fount of knowledge, including a page for the IPointerClick interface.

On that page there is the small example:

using UnityEngine;
using UnityEngine.EventSystems;

public class Example : MonoBehaviour, IPointerClickHandler
{
    //Detect if a click occurs
    public void OnPointerClick(PointerEventData pointerEventData)
    {
        //Output to console the clicked GameObject's name and the following message. You can replace this with your own actions for when clicking the GameObject.
        Debug.Log(name   " Game Object Clicked!");
    }
}

One thing to take particular note of is this instruction:

Ensure an Event System exists in the Scene to allow click detection.

  • Related