0

I am trying to create a very simple button in Unity, using the information provided in this question but after a multitude of attempts it won't seem to work.

I have a "button" object, with a SpriteRenderer and BoxCollider2D component, a Physics2DRaycaster attached to my main camera, and the following code attached to the object:

using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;

public class ButtonController : MonoBehaviour, IPointerClickHandler
{

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }  

    public void OnPointerClick(PointerEventData eventData)
    {
        Debug.Log("Clicked" + eventData.pointerCurrentRaycast.gameObject.name);
    }
}

This is the scene, there is clearly nothing obstructing the button:

enter image description here

There is only the button and the camera in the scene:

enter image description here

And these are the components attached to both the camera and the button:

enter image description here

enter image description here

I'm aware this isn't strictly the kind of question to ask on here but it's been several days now and this should have been something very simple but can not for the life of me see what's gone wrong.

Tom Ryan
  • 397
  • 2
  • 6
  • 26
  • @hellium even if my button is a gameObject? – Tom Ryan Jun 04 '18 at 14:41
  • With a sprite renderer – Tom Ryan Jun 04 '18 at 14:42
  • Read the troubleshooting part. If this is a sprite renderer then the object is 2D which means after attaching `Physics2DRaycaster` to the camera, you must attach any 2D collider to it. `CircleCollider2D` or `BoxCollider2D` should be fine. Also, you don't need to put that Sprite under the canvas. If you have to then consider using the `Image` or `RawImage` component which doesn't require collider at-all. – Programmer Jun 04 '18 at 15:52
  • @Programmer I've done all those things, I've updated the question to make it more clear – Tom Ryan Jun 05 '18 at 12:14
  • Ok. Can you also add screenshot of the Hierarchy tab? – Programmer Jun 05 '18 at 18:10
  • @Programmer there’s one there – Tom Ryan Jun 05 '18 at 18:11
  • I see it now. That means you only have two objects in the scene? Camera and the object you want to detect clicks on? – Programmer Jun 05 '18 at 18:12
  • @programmer yeah – Tom Ryan Jun 05 '18 at 18:13
  • Read the duplicate again. Scroll down to the "Troubleshooting" section. Read part A. It says something about EventSystem and shows how to create that. You must do that – Programmer Jun 05 '18 at 18:15

1 Answers1

0

Do you have an Image component added to your object? For the event to trigger you need to have a component that can be a RaycastTarget, ie, it won't trigger from an empty gameObject, it also needs to be child of Canvas.

If you start from fresh scene and create any UI element from the GameObject menu, Unity will create an Event System, Canvas and InputModule components, which is a great start - if you can click the button, you should get your debug if you click on an image with your script - your code is correct.

For a sprite working with the event system, you'll need a physics raycaster and collider. A 3D Box collider works with Physics Raycaster, and 2D Box Collider works with Physics2D Raycaster, but if no collider is present your sprite won't intercept raycasts, they need colliders to work

zambari
  • 4,797
  • 1
  • 12
  • 22