Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

OnMouseDown not working on IOS

Discussion in 'Scripting' started by sebdub, Jul 6, 2020.

  1. sebdub

    sebdub

    Joined:
    Apr 18, 2015
    Posts:
    2
    Morning, everyone,

    I am taking the liberty of opening a new Thread because despite all my research, I have not found the solution.

    I am developing an app for IOS and I have a problem with void OnMouseDown(); It works fine with the mouse or on my phone with Unity Remote 5 but when I install it on my phone (via xcode) it doesn't work anymore.

    I tried several solutions but nothing works.
    Either the touch is detected but the object is not detected (it moves another one or more) or nothing moves.

    If someone has an idea, that would be great!

    Translated with www.DeepL.com/Translator (free version)

    Translated with www.DeepL.com/Translator (free version)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Block : MonoBehaviour
    6. {
    7.     public event System.Action<Block> OnBlockPressed;
    8.     public event System.Action OnFinishMoving;
    9.  
    10.     public Vector2Int coord;
    11.     Vector2Int startingCoord;
    12.  
    13.  
    14.     public void Init(Vector2Int startingCoord,Texture2D image)
    15.     {
    16.         this.startingCoord = startingCoord;
    17.         coord = startingCoord;
    18.  
    19.         GetComponent<MeshRenderer>().material = Resources.Load<Material>("Block");
    20.         GetComponent<MeshRenderer>().material.mainTexture = image;
    21.     }
    22.  
    23.     public void MoveToPosition(Vector2 target, float duration)
    24.     {
    25.         StartCoroutine(AnimateMove(target, duration));
    26.         print(target);
    27.     }
    28.  
    29.     private void OnMouseDown()
    30.     {
    31.      
    32.            OnBlockPressed(this);
    33.  
    34.     }
    35.  
    36.     IEnumerator AnimateMove(Vector2 target, float duration)
    37.     {
    38.         Vector2 initialPos = transform.position;
    39.         float percent = 0;
    40.  
    41.         while (percent < 1)
    42.         {
    43.             percent += Time.deltaTime / duration;
    44.             transform.position = Vector2.Lerp(initialPos, target, percent);
    45.             yield return null;
    46.         }
    47.         if(OnFinishMoving != null)
    48.         {
    49.             OnFinishMoving();
    50.         }
    51.     }
    52.  
    53.     public bool IsAtStartingCoord()
    54.     {
    55.         return coord == startingCoord;
    56.     }
    57. }
    58.  
     
  2. sebdub

    sebdub

    Joined:
    Apr 18, 2015
    Posts:
    2
    I may have found a solution!
    I couldn't put a collider on my prefab because there was a MeshCollider.

    So I first removed the MeshCollider :
    Code (CSharp):
    1. DestroyImmediate(GetComponent<MeshCollider>(), true);
    Then I added a Boxcollider :
    Code (CSharp):
    1. Object.AddComponent<BoxCollider>();
    According to the first test it works once installed on my phone.