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 kofexStudios · Aug 15, 2017 at 03:28 PM · unity 5eventsystemprefab-instance

Click Events in Instantiated prefab does not work

Hi, I have a prefab that I instantiate at a point in my game. The script attached to the prefab has the public void OnPointerDown(PointerEventData eventData) function implemented. Now when the prefab is dragged into the scene manually (before the game is run), the click event is registered. However when the prefab is instantiated during runtime, the click event is not registered. What could be the problem?

Comment
Add comment · Show 2
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 roddles · Sep 19, 2017 at 06:07 PM 0
Share

I am using Unity 5.6.3p2, and am encountering the same issue. Am submitting this to Unity as a bug.

Here's my PointerClickable script:

 public class PointerClickable : $$anonymous$$onoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler {
     public GameObjectEvent PointerClickEvent;
 
     void Awake () {
         if (PointerClickEvent == null) {
             PointerClickEvent = new GameObjectEvent ();
         }
     }
     
     #region IPointerClickHandler implementation
 
     public void OnPointerClick (PointerEventData eventData) {
         PointerClickEvent.Invoke (gameObject);
         Debug.Log ("Hello, Toggle.");
     }
 
     #endregion
 
     #region IPointerDownHandler implementation
 
     public void OnPointerDown (PointerEventData eventData) {
         // No-op; interface/impl are necessary to get IPointerClickHandler to work
     }
 
     #endregion
 
     #region IPointerUpHandler implementation
 
     public void OnPointerUp (PointerEventData eventData) {
         // No-op; interface/impl are necessary to get IPointerClickHandler to work
     }
 
     #endregion
 }

$$anonymous$$y prefab has an empty root object and child object with mesh, mesh renderer, collider, and this script.

Prefab instantiated scenario: 1. Run app in Unity 2. Another script instantiates prefab into scene 3. Tap instantiated game object

Actual Result: Tap is not detected (no message, no event raised) Expected Result: Tap is detected.

Prefab instantiated scenario: 1. Run app in Unity 2. Another script instantiates prefab into scene 3. Tap instantiated game object

Result: Tap is detected (debug message is logged, event is raised).

avatar image roddles roddles · Sep 20, 2017 at 12:49 AM 0
Share

After trying to replicate this behavior in a $$anonymous$$imal app, turns out that the instantiated prefabs were properly sending the OnPointerClick message.

Further debugging revealed that it was a combination my transform's y-scale was zero'd out, making the object (a plane) infinitely thin, such that the ray caster was not intersecting it.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by carlos170586001500 · Aug 15, 2017 at 03:40 PM

Try to add print() or Debug.Log()

 using UnityEngine.EventSystems;
     ...
 public void OnPointerDown(PointerEventData eventData){
     Debug.Log("OnPointerDown Event Work");
     //Your code
     Debug.Log("OnPointerDown Event Work. Your code too");
 }


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 kofexStudios · Aug 15, 2017 at 04:08 PM 0
Share

I have tried printing something but nothing shows in the console.

avatar image carlos170586001500 kofexStudios · Aug 15, 2017 at 04:27 PM 0
Share

Add IPointerClickHandler:

  public class TaskIcon : $$anonymous$$onoBehaviour,IPointerClickHandler{
       ....
avatar image kofexStudios carlos170586001500 · Aug 16, 2017 at 10:08 AM 0
Share

This is what I have currently:

 public class $$anonymous$$yClass: $$anonymous$$onoBehaviour, IPointerClickHandler{
      
    
       public void OnPointerClick(PointerEventData eventData){
           print("object is clicked on with another event handler");
       }
 }


And it's still not working

Let me also reiterate that, this script is attached to a prefab that gets instantiated in runtime. When I put the prefab manually in the scene, the click event works. But does not work when I instantiate in runtime. I don't get why this happens.

avatar image
0

Answer by Omar_siddiqui015 · Aug 15, 2018 at 12:28 PM

this is the only solution for instantiate,you can instantiate a child by onclick,only by inspector..)here is your solution in the picture

alt text


pics2.png (372.3 kB)
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

140 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 avatar image avatar image avatar image

Related Questions

can't use EventSystems, Reference is missing 1 Answer

Instantiating a new clone of a prefab whilst setting the previous clone as inactive 0 Answers

OnDrag does not stop to detect screen bounds 0 Answers

Unity 5 UI buttons don't respond well with clicks when used in windows standalone with different resolutions. 0 Answers

Hi I am trying to assign gameobject (prefab) from the List to another prefab gameobject but i think there is some mistake in one line of my code Please help. 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