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);
// }
}
}