- Home /
Trying to attach OnPointerEnter script to prefab
This is probably a common problem, but I'm trying to attach a script to a prefab that is instantiated during run time. The script is linked below. Basically, I want the script to enable an inactive game object once the cursor hovers over the newly instantiated prefab. This script works on GameObjects that are already in my hierarchy, but it doesn't seem to work for prefabs.
If anyone has a better idea on how to approach this I would appreciate it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class TextTest : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
// Start is called before the first frame update
public GameObject HoverUI;
void Start()
{
HoverUI.SetActive(false);
}
public void OnPointerEnter(PointerEventData eventData)
{
HoverUI.SetActive(true);
Debug.Log("Test");
}
public void OnPointerExit(PointerEventData eventData)
{
HoverUI.SetActive(false);
}
}
Instantiated prefabs are also part of the hierarchy, if the prefab has the script, once it is instantiated and enabled it should receive pointer events perfectly fine.
hi
well, many of us probably have already done a UI item manager(like the bag of the player), and there is mandatory manage that specific events (OnPointerEnter, OnPointerExit and OnDrag) and believe me, that works fine with items instantiated procedurally, so the problem is in other site.
some of the probably sites:
you didnt add a eventsystem manager to your hierachy, usually it is crated automatically when you add a canvas, or any UI element on the scene, but not in the case you do all by script or procedurally
the parent of this object is outside (not a child of) the canvas hierachy, thats not pretty probably, you know, usually you cant see the text in this case
raycast target on text options in the prefab is disabled
hope this helps, good luck