Using OnPointerClick with Buttons
Presently I'm using a separate Script attached to each Button in order to capture clicks and run a function in my main script.
The problem I'm having is that while using the left button, handled by "PointerEventData.InputButton.Left" my function works perfectly, but using "PointerEventData.InputButton.Right" causes the function to return a NullRefferenceException. Yet, the .Right code works if I first left click the Button, and then Right click it. (both with the .Left code removed and when it's there)
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class ClickableObject : MonoBehaviour, IPointerClickHandler
{
public void OnPointerClick(PointerEventData eventData)
{
GameObject _GameControlerObj = GameObject.Find("_GameControlerObj");
_GameControler gc = _GameControlerObj.GetComponent<_GameControler>();
if (eventData.button == PointerEventData.InputButton.Left)
{
gc.DoSomething();
}
else if (eventData.button == PointerEventData.InputButton.Right)
{
gc.DoSomething();
}
}
}
I tried removing the Button component and adding a 2DBoxColider, just caused it to return exceptions on both left and right clicks.
I'm completely lost as to how to fix it, any ideas?
If it matters, the buttons are all created through script, and set to be children of a canvas. Event System is present as a child of the Canvas as well
Are you sure the null reference is not for the _GameControlerObj ? You should really move that GameObject.Find in an Awake or Start function. It's heavy, you don't want to be doing that every time you click.
Thanks for the suggestion, but sadly that wasn't whats breaking it
thanks man. you're question indirectly answered one of my own :)
Answer by finalsin · Jan 11, 2016 at 11:45 PM
If anyone else is having this problem, it turns out that Unity sometimes doesn't feel like allowing you to update through the in-editor button. Updating to 5.3.1 fixed the issue
Your answer
Follow this Question
Related Questions
UIElements button MouseDownEvent doesn't appear 1 Answer
Does not contain definition of isTrigger? 1 Answer
Can't find StartHost() function for a button 1 Answer
How to creating a Gameobject from a prefab via scripted UI BUTTON 1 Answer
Button mouseover only works when other canvas is active 0 Answers