Search Unity

[SOLVED] OnPointerDown not working on Device but working in Editor

Discussion in '2D' started by ZliVuk, Dec 21, 2019.

  1. ZliVuk

    ZliVuk

    Joined:
    Mar 24, 2018
    Posts:
    61
    This one is odd.
    Gameobject works with OnPointerDown in Editor (when i hit play) as it should (using mouse).
    Without any change apk is generated and same object does not react to OnPointerDown when touch is on screen.

    Gameobject is not UI (has Transform, Sprite Renderer, Animator, Circle Collider 2D, Rigidbody 2D and script). Script has:
    - using UnityEngine.EventSystems;
    - public class myGameObject : MonoBehaviour, IPointerDownHandler
    - public void OnPointerDown(PointerEventData click)
    and simple animation start at random and some force added also random.
    Code (CSharp):
    1.  
    2. private void OnEnable() {
    3. gameObject.GetComponent<Animator>().Play("myGameObject_animation", -1, Random.Range(0.0f, 1.0f));
    4. gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2( UnityEngine.Random.Range(-1.0f,-0.1f), UnityEngine.Random.Range(0.5f,1.5f) ), ForceMode2D.Impulse);
    5. }
    6.  
    Camera does have:
    - Physics 2D Raycaster - With yellow exclamation mark and error: Physics2D module is not present. This Raycaster will have no effect
    - Event Mask: Everything
    - Max Ray intersection: 4

    Camera is on Layer: Default
    Gameobject is on Layer: Last
    Gameobject is on Sorting Layer: Last

    There are few more gameobject in same Scene with OnPointerDown and all of them work without problems on different layers (and Sorting layers) and other options too.

    Everything works in Editor Play mode but on Android device this one gameobject does not.
    Linux (Manjaro), 2019.3.0f3

    What am I missing?
     
    Last edited: Dec 21, 2019
  2. ZliVuk

    ZliVuk

    Joined:
    Mar 24, 2018
    Posts:
    61
    If anybody comes with similar issue, in my case problem was calling referenced object from Instantiated (Instance, DoNotDestroy) object. MyGameobject that did not respond to OnPointerDown that had reference to function in script of that referenced gameobject from Instance. Since MyGameObject was prefab from Pool that was generated (instantiated) on Scene load, and reference was created from same script that created Pool, in Editor difference in script loading in Project Settings/Scripc Execution Order of 75ms was enough when running on PC but on mobile was not so MyGameObject that was calling referenced gameobject from Start() was missing reference and nothing happened. Once I moved calling referenced gameobject from Start() to OnEnable() (MyGameObject from Pool is in disable state, when MyGameObject was actually used in scene it gets activated/enabled) everything stated working as it should.

    Timings in execution of scripts on Editor (PC) and mobile device are not the same and absolute. The fact that script stats before some other does not mean that it will finish before second starts.

    That means that it will take longer to execute some scripts on device so when next script in line uses references from previously started script you have to take into account that previous script did not finish and maybe referenced object is not referenced or reference cannot be used when instantiating prefabs from second script that use referenced gameobject in Start().

    Hope this makes sense and helps someone.
     
    Piero_Diaz and ScottAdams like this.