- Home /
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?
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).
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.
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");
}
I have tried printing something but nothing shows in the console.
Add IPointerClickHandler:
public class TaskIcon : $$anonymous$$onoBehaviour,IPointerClickHandler{
....
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.
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