Wait for mouse input from user, then repeat the program

Hi, I wanted to ask how can I achieve that the program is constantly waiting for the input, and after receiving input, it loops again from the beginning? Here’s my code :

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UIElements;

public class ColliderManager : MonoBehaviour{
    GameObject[] child;
    GameObject RandomChild;
    int index;
    int amountOfClicks = 0;
    void FixedUpdate(){
        OnMouseDown();
    }

    void OnMouseDown(){
        Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);
        RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero);
        if (Input.GetMouseButtonDown(0)){
            Debug.Log(hit.collider.gameObject.name);
            if (hit.transform.gameObject == RandomChild){
                RandomChild.SetActive(false);
                amountOfClicks++;
                Debug.Log("Random child is now inactive\n");
            }
        }
    }

    void Start(){
        //while(amountOfClicks != 5){
            child = GameObject.FindGameObjectsWithTag("Collision_Collider");
            index = Random.Range(0, child.Length);
            RandomChild = child[index];
            for (int i = 0; i < child.Length; i++){
                if (child[i] == RandomChild){
                    child[i].SetActive(true);
                }
                else{
                    child[i].SetActive(false);
                }
                Debug.Log("Child is " + child[i].name);
            }
            Debug.Log("Random child is " + RandomChild.name);
       // }
    }
}

When checking for Input, you should do so in Update and not FixedUpdate.

Otherwise, just put your code into a method that you want to run again and then call the method when a user triggers the proper input.

Also, OnMouseDown is a unity special method and may not be what you’re wanting to use.

1 Like

So what should I use instead of OnMouseDown please?

The standard Unity input doesn’t rely on input events as you might expect it if you’re coming from some other programming environment. The newer input system is made differently, but prior to that you would simply check for state of an input in the Update (so called ‘immediate mode’ which is more or less like a waterfall).

Of course, you can make a more elaborate system with it, make your own input registration class and then propagate custom events as you see fit.

In your case however this is the mechanism you’re after

using UnityEngine;

public class MyClass : MonoBehaviour {

  // Update is a special method, a message callback, that comes with MonoBehaviour
  // there are quite a few of these, so make sure to check out the documentation
  // if you're acquainted with OOP these do not work like typical overrides, but are written standalone
  // unlike overrides, if a message callback doesn't exist it won't be called at all, nor attempted to be called
  // this particular method will be called every frame, IF the hosting object is active and this script is enabled (see docs)
  void Update() {
    if(Input.GetMouseButtonDown(0)) {
      Debug.Log("mouse button was pressed");
      doSomething();
    }
  }

  // this is what you want to be done every time the user presses the button
  void doSomething() {
    Debug.Log("something was done");
  }

}

Attach this script to a game object in your scene.
You probably haven’t noticed the difference between GetMouseButton and GetMouseButtonDown.