Search Unity

Can't detect OnPointerDown on a nested instantiated prefab

Discussion in 'Prefabs' started by PetyrVeliki, Aug 5, 2021.

  1. PetyrVeliki

    PetyrVeliki

    Joined:
    Jul 8, 2021
    Posts:
    3
    I dynamically create instance of a prefab - called Animal. And in some cases - I dynamically instantiate nested prefab inside the Animal - called Carrot.

    Both Animal and Carrot implement IPointerDownHandler and have OnPointerDown methods:

    Code (CSharp):
    1.  
    2. // In Animal.cs ..
    3. public void OnPointerDown(PointerEventData data)
    4. {
    5.     Debug.Log("POINTER DOWN IN ANIMAL !!!" + data);
    6. }
    7. // In Carrot.cs ..
    8. public void OnPointerDown(PointerEventData data)
    9. {
    10.     Debug.Log("POINTER DOWN IN CARROT !!!" + data);
    11. }
    However - the one in carrot never gets called. I always get the debug log from the Animal instance.

    Any idea why I can't detect the pointer down on both so that I know if the user clicked/tapped the carrot or the animal that holds it?
     
  2. PetyrVeliki

    PetyrVeliki

    Joined:
    Jul 8, 2021
    Posts:
    3
    At the end of the day - the listener in the carrot didn't fire at all :( ... even if I remove the other listener.

    So - I ended up with converting the PointerEventData parameter data into a world position and compare it with the transform.position of the Animal - and thus specify whether the user has tapped on the animal or on the carrot.

    Dumb ... but working :)