OnMouseDown work around

The OnMouseDown methods as said in the course does not work. I have looked around and seen that this is a common problem and that unity basicly doesn’t support it anymore ?

So far I have found the “OnPointerClick” method from what I understand you need a EventSystem in your Hierarchy with a direction in your code + Physics Raycaster on your camera by code so it can track your mouse movement.

My code for it looks like this for it now:

‘’’
using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.EventSystems;

public class Waypoint : MonoBehaviour,

{

PointerEventData = eventData

void Start()

{

    addPhysicsRaycaster();

}

//Allows your camera to track mouse movement and actions

void addPhysicsRaycaster()

{

    PhysicsRaycaster physicsRaycaster = GameObject.FindObjectOfType<PhysicsRaycaster>();

    if (physicsRaycaster == null)

    {

        Camera.main.gameObject.AddComponent<PhysicsRaycaster>();

    }

}

//Alternative OnMouseDown method

void OnPointerClick(PointerEventData eventData)

{

    Debug.Log(transform.name);

}

}

‘’’

that sadly still does nothing. I DID read something else about needing “IPointerClickHandler” in your class as well (I assume that just makes it understand you clicking ?) but that just gives me the following error that i don’t really understand:

Blockquote

Assets\Tile\Waypoint.cs(6,40): error CS0737: ‘Waypoint’ does not implement interface member ‘IPointerClickHandler.OnPointerClick(PointerEventData)’. ‘Waypoint.OnPointerClick(PointerEventData)’ cannot implement an interface member because it is not public.

Blockquote

Beside that all I can add is that i do have Box Colliders on my object and that i have tried to place them both a bit above and beneath my terrain.

As always it will very likely be something small I just don’t see or miss understood but at this point I could use some help figuring out what it is.

1 Like

Works for me.

If I’m not mistaken, this is a UI event handler.

That is all I needed, Concluded too fast it didn’t work and tried to find a complex different way.

Turns out I placed the boxcolider on the Child tile instead of the Root object that had the Script.

2 Likes

I’ve done that before.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms