Refrence another text object from within a script involving PointerEnter and PointerExit
So I've checked around and normally I'm able to find this stuff, or find a work around.
For reference-
I have 3 UI objects, one is a button called "Up Kills", and its subsequent text "Upkilltext"
The other object is a text object named "Hahlol".
Up Kills has an EventTrigger script attached to it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using UnityEngine.EventSystems;
public class UpgradeKillButton : EventTrigger {
//Text UpInfo = GameObject.Find("Hahlol").GetComponent[UnityEngine.UI.Text](); ??????
public override void OnPointerEnter(PointerEventData data)
{
UpInfo.text = ("Lol JK it does nothing");
}
public override void OnPointerExit(PointerEventData data)
{
UpInfo.text = ("");
}
}
The commented line is the one I'm having trouble with.
Usually I just use a [SerializeField] and call it a day, (Just learning here, expecting to find a better way later.)
But that wont show up in the Inspector.
So what I'm attempting to do, is change the text of "Hahlol" whenever the mouse has entered the button, and upon leaving, remove the text.
Not looking for a copy-paste answer, if you can avoid it.
Thanks.
Answer by Hellium · May 11, 2019 at 07:35 PM
Remove the EventTrigger
component attached to your button, and change the script as follow:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using UnityEngine.EventSystems;
public class UpgradeKillButton : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
[SerializeField]
private Text UpInfo ;
public void OnPointerEnter(PointerEventData data)
{
UpInfo.text = "It works now!";
}
public void OnPointerExit(PointerEventData data)
{
UpInfo.text = null;
}
}
Make sure you have an EventSystem
component attached to a gameObject in your hierarchy.
Tried all different methods of making this work, I added a second event system, just to see if i needed it (nope.), with each change I'm getting this error:
I tried setting it up using IPointerEnterHandler before using this as refrence: https://docs.unity3d.com/ScriptReference/EventSystems.IPointerEnterHandler.html Though i think i was getting a different error when doing that, so, on the right track, maybe.
$$anonymous$$y bad, remove the override
keyword.
Your answer
Follow this Question
Related Questions
Dialogue not displaying next sentence 0 Answers
How to make conversation UI similar to Pillars of Eternity or Planescape:Torment 0 Answers
Why my pointer events keep triggering 3 times all the time??? 0 Answers
How to disable button text UI 0 Answers
Problem with highlighting correct answer, any idea? 1 Answer