Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Arcana96 · Dec 11, 2016 at 12:27 PM · scripting problemuieventsystemevent-handling

IPointer Events don't seem to be working

Hello, I've tried all of the different events and the don't seem to be working. I've been inheriting from IPointerClick Hander and implementing the interface but nothing seems to be working. Any ideas?

The code for reference:

 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 using System.Collections;
 using System;
 
 public class Slot : MonoBehaviour, IPointerClickHandler
 {
     public void OnPointerClick(PointerEventData eventData)
     {
         Debug.Log("Clicked");
     }
 }

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by tanoshimi · Dec 11, 2016 at 12:39 PM

Is there an eventsystem in your scene? Does your camera have a physics raycaster on it? (Or does the canvas in which this element is contained have a graphic raycaster on it?)

Comment
Add comment · Show 2 · 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 Arcana96 · Dec 11, 2016 at 12:44 PM 0
Share

Yeah, I have an EventSystem in the scene and the Canvas has a Graphics raycaster on it.

avatar image danieltanfh95 · Nov 16, 2020 at 11:05 PM 0
Share

exactly the problem i have.

avatar image
0

Answer by Pengocat · Dec 11, 2016 at 12:51 PM

Perhaps something is blocking the thing you are trying to click. When in play mode highlight the EventSystem and look at it in the inspector. If the name of the thing you want to click is not showing up then it is never called.

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 Bunny83 · Dec 11, 2016 at 01:32 PM

The GameObject where your script is attached to has to have another component attached that is derived from UI.Graphic. Like one of the built-in components: Image, RawImage or Text. Only GameObjects which have a Graphic attached can actually be interacted with since the GraphicsRaycaster, as the name suggests, can only detect Graphics.

Graphic is an abstract class. However it adds itself to a global Graphic registry which is used by the raycaster. So only GameObjects which are registrated in that manager can actually be "hit".

You can create a pure event target class like this:

 //EventTarget.cs
 using UnityEngine.UI;
 
 public class EventTarget : Graphic
 {
     protected override void OnPopulateMesh(VertexHelper vh)
     {
         vh.Clear();
     }
 }

Just attach it to the same GameObject and you will receive your events. This is basically an empty Graphic as it does not generate any triangles / vertices. So this is a bit more efficient than using an Image with alpha set to 0 as it would still generate the quad and it would still be rendered.

Comment
Add comment · Show 3 · 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 Bunny83 · Dec 11, 2016 at 01:38 PM 0
Share

Even though it's not necessary, usually you would derive UI components from UIBehaviour ins$$anonymous$$d of $$anonymous$$onoBehaviour. It doesn't really implement any functionality, but implements some virtual methods which can be overridden. If you use VisualStudio, just type "override" and a space inside your class body and it should list all methods that can be overridden.

avatar image Arcana96 · Dec 11, 2016 at 01:52 PM 0
Share

That doesn't work but adding graphics raycaster to the ui does. For some reason my ui object has a "canvas" component attached to it and when I remove it my object is no longer visible. I didn't add it.

avatar image Bunny83 Arcana96 · Dec 11, 2016 at 02:36 PM 0
Share

Um, any UI component has to be under a canvas object. Is the object you have your script attached to a child somewhere below a canvas object?

$$anonymous$$y test scene currently has this layout:

 -Canvas
     |
     \-- $$anonymous$$yTestObject
 -EventSystem

Where the Canvas object has the components:

  • RectTransform

  • Canvas

  • CanvasScaler

  • GraphicRaycaster

The EventSystem has those:

  • Transform

  • EventSystem

  • StandalongInput$$anonymous$$odule

And finally $$anonymous$$yTestObject has:

  • RectTransform

  • Your "Slot" script

  • $$anonymous$$y EventTarget script

  • CanvasRenderer (has been added automatically)

For me it works just fine. The canvas as well as the EventSystem has been generated by Unity automatically. If you remove the canvas and the eventsystem and just do Create->UI->Text or something similar it should create a canvas, but an object with a Text component as child and add the eventsystem to the scene.

I just removed the Text gameobject and created a new empty gameobject inside the canvas. I attached both scripts to it and positioned it properly on the screen so i can actually click on it. When i test the scene it works just fine.

I'm a bit confused. You said the object is no longer visible. "What" actually is visible? What other UI components do you use and how are they arranged? $$anonymous$$eep in $$anonymous$$d that the "IPointerClickHandler" and similar interfaces are only ment for 2d UI stuff. Can you post a screenshot of your sceneview / project panel so we can see your arrangement?

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

102 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

Related Questions

Unity 2019.3 any way to make mouse events pass thru an UI element without disabling "Raycast Target" ? 2 Answers

How can i add a event onclick for a ui button that is child of canvas ? 1 Answer

Help, Use event system IPointerClickHandler 1 Answer

How to send mouse events directly to the canvas in world pos 0 Answers

UnityEngine.EventSystems.. Trying to figure it out .js 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