- Home /
Unusual trouble with OnPointerClick
I have been using the OnPointerClick method for some UI Icons so that the building will spawn at the cursor and then can be placed on the world mesh.
For some reason unknown to me, the OnPointerClick method will not trigger anymore. I have the EventSystem in the scene, I have a box collider on the icon. This was working and I have no idea how I broke it.
I have multiple canvases in the scene (each unit has a canvas with a healthbar). Only one EventSystem. The EventSystem doesn't even appear to be logging PointerEnter events in the inspector. Also, the only Button object on the main canvas disables itself randomly.
I have completely broken my UI and had no idea it was happening.
Code sample below: using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems;
public class BuildButton : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler
{
public SpawnBuilding buildingPrefab;
BoxCollider2D collider;
Player player;
public int buildingType;
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log("Click");
}
public void OnPointerUp(PointerEventData eventData)
{
Debug.Log("Click");
}
public void OnPointerClick(PointerEventData eventdata)
{
Debug.Log("Click");
//Instantiate(buildingPrefab);
if(buildingType == 1)
{
GameObject building = PhotonNetwork.Instantiate("buildingPrefab 1", eventdata.pointerCurrentRaycast.worldPosition, Quaternion.identity, 0);
}
else
{
GameObject building = PhotonNetwork.Instantiate("buildingPrefab 2", eventdata.pointerCurrentRaycast.worldPosition, Quaternion.identity, 0);
}
//buildingPrefab.transform.position = eventdata.pointerCurrentRaycast.worldPosition;
}
}
Answer by Raziid · Jun 13, 2017 at 10:39 PM
I solved the problem after a bunch of debugging.
Somehow, canvases got placed on all the icons so they weren't interpreting the event system properly. Hopefully this helps someone
Your answer
Follow this Question
Related Questions
How to detect which UI Button was triggered via Script (and not actually clicked)? 0 Answers
Highlighted buttons stay highlighted when I deactivate a canvas. No way to unhighlight them. 1 Answer
Can't interact with buttons in a UI Canvas in World Space Render Mode. 0 Answers
UI Buttons visible but not clickable when using two canvases. 3 Answers
UI elements stop working based on hirarchy position 1 Answer