Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Help get Button after pointerEnter

Discussion in 'UGUI & TextMesh Pro' started by smokegun, Mar 9, 2015.

  1. smokegun

    smokegun

    Joined:
    Dec 13, 2012
    Posts:
    170
    Hello,

    I am trying to get my button object after my pointer enters it.
    My code:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.EventSystems;
    4. using System.Collections;
    5.  
    6. public class eventButton : MonoBehaviour {
    7.  
    8.     public void PointerEnter(BaseEventData eventData){
    9.         print ("It's me: " + eventData.selectedObject.name);
    10.     }
    11. }
    12.  
    Error (When hover):
    NullReferenceException: Object reference not set to an instance of an object
    eventButton.PointerEnter (UnityEngine.EventSystems.BaseEventData eventData) (at Assets/Menno/Scripts/eventButton.cs:9)
    UnityEngine.Events.InvokableCall`1[UnityEngine.EventSystems.BaseEventData].Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:141)
    UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:574)
    UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:716)
    UnityEngine.Events.UnityEvent`1[T0].Invoke (.T0 arg0) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_1.cs:53)
    UnityEngine.EventSystems.EventTrigger.Execute (EventTriggerType id, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/EventTrigger.cs:52)
    UnityEngine.EventSystems.EventTrigger.OnPointerEnter (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/EventTrigger.cs:59)
    UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerEnterHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:24)
    UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerEnterHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269)
    UnityEngine.EventSystems.EventSystem:Update()

    Please help
     
  2. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
    Will have a look around, but I suspect it's because an object is not "selected" by just passing the mouse over it.
    If you attach that script to a button GO, then you should only need do:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3.  
    4. public class eventButton : MonoBehaviour, IPointerEnterHandler
    5. {
    6.     public void OnPointerEnter(PointerEventData eventData)
    7.     {
    8.         print("It's me: " + name);
    9.     }
    10. }
    *note, reRead your code while testing and that script would never work. Simply because it does not implement the IPointerEnterHandler interface. Not sure what you got working there.

    Hope this helps.
     
  3. smokegun

    smokegun

    Joined:
    Dec 13, 2012
    Posts:
    170
    Thank you but i'm affraid this doesn't work because i can't select the function anymore.

     
  4. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
    Ahh, you are doing it that way :D. That explains it.
    1. Remove EVENTTRIGGER - you shouldn't use this unless you really need to.
    2. Add the script to your object directly.
    if however you wish to use EventTrigger (#shudder), then you only need update your script as follows:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3.  
    4. public class eventButton : MonoBehaviour
    5. {
    6.     public void PointerEnter(BaseEventData eventData)
    7.     {
    8.         print("It's me: " + name);
    9.     }
    10. }
    I've confirmed that the selectedObject property is only populated when you Click on the button or enter an Inputfield or toggle (or any other interactable object)
     
    smokegun likes this.
  5. smokegun

    smokegun

    Joined:
    Dec 13, 2012
    Posts:
    170
    Thanks it works!!

    But there is another question :D

    I want to change a value in the components of the button "the color".
    So if i use:
    Code (CSharp):
    1. gameObject.GetComponent<Image> ().color = Color.red;
    It says that image doesn't exists?
     
  6. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
    Hmm, I just answered this in another thread but here goes :D.
    Don't try and query the component in the event action, that can cause issue.
    Best to get a reference to the Image at the script start and then update that reference in the event, as follows:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3.  
    4. public class eventButton : MonoBehaviour
    5. {
    6.     private UnityEngine.UI.Image myImage;
    7.     void Start()
    8.     {
    9.         myImage = GetComponent<UnityEngine.UI.Image>();
    10.     }
    11.     public void PointerEnter(BaseEventData eventData)
    12.     {
    13.         print("It's me: " + name);
    14.         myImage.color = Color.red;
    15.     }
    16. }
    (same update should work for the previous example)
    But I would seriously avoid using EventTrigger, it has a huge performance drain, especially if you use it everywhere. It is only meant for a few small scenarios.
     
  7. smokegun

    smokegun

    Joined:
    Dec 13, 2012
    Posts:
    170
    Hmm, That sounds bad so whats then the best way to change the image color of a button when you hover it?
     
  8. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
  9. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    Why do you say this? Isn't the EventTrigger component simply a shortcut to implementing the interfaces explicitly?
     
  10. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
    :D Event trigger is like any other script EXCEPT it implements ALL of the interfaces. Which means it is affected for each and every event that fires, instead of just those you care about.
    So whether or not you actually configure each event, the EventSystem will fire every event at an object with an EventTrigger on it.

    Not too bad if there are only a few, use it lots and you are going to start seeing a performance hit.

    At least that has been my experience.