- Home /
OnSelect and OnPointerEnter work in one script but not another on the same object
I have a series of buttons that when hovered over are supposed to deactivate a block of text in the same panel. I used to have different script attached to each of my buttons. Below is one such script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Challenge1Hover : MonoBehaviour, ISelectHandler, IDeselectHandler, IPointerEnterHandler, IPointerExitHandler
{
public GameObject challenge1Description;
public void OnSelect(BaseEventData eventData)
{
challenge1Description.SetActive(true);
Debug.Log("challenge 1 selected");
}
public void OnDeselect(BaseEventData eventData)
{
challenge1Description.SetActive(false);
Debug.Log("challenge 1 deselected");
}
public void OnPointerEnter(PointerEventData pointerEventData)
{
challenge1Description.SetActive(true);
Debug.Log("challenge 1 hovered over");
}
//Detect when Cursor leaves the GameObject
public void OnPointerExit(PointerEventData pointerEventData)
{
challenge1Description.SetActive(false);
Debug.Log("challenge 1 unhovered over");
}
}
These worked fine, but I wanted to consolidate these into one script that I could attach to all of my buttons, so I made this one and attached it to each of them:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Linq;
public class ChallengeButtonHover : MonoBehaviour
{
public GameObject challengeDescription;
List<string> objectName;
private string challengeNumber;
void Awake()
{
objectName = gameObject.name.Split(' ').ToList();
challengeNumber = objectName[1];
challengeDescription = GameObject.Find("Challenge " + challengeNumber + " Description");
challengeDescription.SetActive(false);
Debug.Log("My description is for challenge " + challengeNumber);
}
public void OnSelect(BaseEventData eventData)
{
challengeDescription.SetActive(true);
Debug.Log("Challenge " + challengeNumber + " selected");
}
public void OnDeselect(BaseEventData eventData)
{
challengeDescription.SetActive(false);
Debug.Log("Challenge " + challengeNumber + " deselected");
}
//Detect when Cursor enters the GameObject
public void OnPointerEnter(PointerEventData pointerEventData)
{
challengeDescription.SetActive(true);
Debug.Log("Challenge " + challengeNumber + " hovered over");
}
//Detect when Cursor leaves the GameObject
public void OnPointerExit(PointerEventData pointerEventData)
{
challengeDescription.SetActive(false);
Debug.Log("Challenge " + challengeNumber + " unhovered over");
}
}
This second second script does not work. I get no errors at all, and in the inspector the description they are supposed to state-toggle are correctly referenced. I have no idea why this shouldn't work.
Answer by Gungnira · Dec 28, 2020 at 06:09 AM
You missed to add ISelectHandler, IDeselectHandler, IPointerEnterHandler, IPointerExitHandler behind your Monobehaviour.,you missed ISelectHandler, IDeselectHandler, IPointerEnterHandler, IPointerExitHandler in your second script
Answer by Eno-Khaon · Dec 25, 2020 at 12:54 AM
While there's fundamentally nothing wrong with your function declarations, you're overlooking an important detail:
Why *DOES* it work in the first script?
The interfaces you're utilizing in your class declaration are being utilized externally to your script(s) to be called in certain situations. Namely...
ISelectHandler, IDeselectHandler, IPointerEnterHandler, IPointerExitHandler
These all add support for those specific functions to be called from other classes which access your script by *those* names. For example, a MonoBehaviour includes functions like Start() and Update() that will optionally be called at runtime, whereas an ISelectHandler will require OnSelect() to be present.
In the end, the solution is rather simple:
public class ChallengeButtonHover : MonoBehaviour, ISelectHandler, IDeselectHandler, IPointerEnterHandler, IPointerExitHandler