Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Mistyjello · Dec 23, 2020 at 09:49 PM · buttonfunctionsetactivebutton trigger events

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

137 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

My button does not assign any variable value,My OnClick button works with Debug.Log, but doesnt assign any values it should 0 Answers

I want to change the fire button when I change the weapon. 0 Answers

Reset a UI button to a default state when it is disabled 1 Answer

works with gameObject but not with Image SetActive vs Enabled 1 Answer

Attach a function to different buttons and pass different parameters 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges