Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Wait for multiple clicks

Discussion in 'Scripting' started by Zavalichi, May 7, 2019.

  1. Zavalichi

    Zavalichi

    Joined:
    Oct 13, 2018
    Posts:
    162
    Hello,

    I want to get two points from global space and for this I have to press click.
    I want to stop the execution until i get these two points.
    The problem is, my update method never get these points.
    On console I see "Done" and "Here1" but never "here2"
    Code (CSharp):
    1.  private void Update()
    2.     {
    3.         if (_buttonPressed && !_stop)
    4.         {
    5.             switch ((int)_btn)
    6.             {
    7.                 case 0:
    8.                     {
    9.                         break;
    10.                     }
    11.                 case 1:
    12.                     {
    13.                         _text.SetActive(true);
    14.                         Vector3 startMousePosition = Vector3.zero;
    15.                         Vector3 endMousePosition = Vector3.zero;
    16.  
    17.                         List<string> messages = new List<string>();
    18.                         messages.Add("Select start point");
    19.                         messages.Add("Select end point");
    20.  
    21.                         if (_mouseClickReady)
    22.                         {
    23.                             StartCoroutine(mouseClick(2, messages, (positions) =>
    24.                             {
    25.                                 if (positions.Count == 2)
    26.                                 {
    27.                                     startMousePosition = positions[0];
    28.                                     endMousePosition = positions[1];
    29.                                     Debug.Log("here1");
    30.                                 }
    31.                             }));
    32.                         }
    33.                         else if ((startMousePosition != Vector3.zero) && (endMousePosition != Vector3.zero))
    34.                         {
    35.                             Debug.Log("here2");
    36.                             Waypoint startPoint = getNearestWaypoint(startMousePosition);
    37.                             Waypoint startEnd = getNearestWaypoint(endMousePosition);
    38.  
    39.                             StartCoroutine(Delay());
    40.                             _mouseClickReady = true;
    41.                             _buttonPressed = false;
    42.                             _btn = ButtonPressed.None;
    43.                             StopPathButton.SetActive(false);
    44.                         }
    45.                         break;
    46.                     }
    47.                 default:
    48.                     break;
    49.             }
    50.         }
    51.     }
    Code (CSharp):
    1.  IEnumerator mouseClick(int n,List<string> message, System.Action<List<Vector3>> callback)
    2.     {
    3.         _mouseClickReady = false;
    4.         List<Vector3> positions = new List<Vector3>();
    5.         int i = 0;
    6.         while (i < n )
    7.         {
    8.             _text.GetComponent<Text>().text = message[i];
    9.             if (Input.GetMouseButtonDown(0))
    10.             {
    11.                 i++;
    12.                 positions.Add(Input.mousePosition);
    13.                 if (positions.Count == n)
    14.                 {
    15.                     _text.GetComponent<Text>().text = "Done";
    16.                     callback(positions);                                                    
    17.                 }
    18.             }
    19.             yield return null;
    20.         }    
    21.     }
     
    Last edited: May 7, 2019