I want to make a sort-of troop placing system, so whenever I click the button, I get to choose where to place the troop and click to place it in that fixed position; however, Unity just freezes every time I click it, and I don’t know what to do. Thanks!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class placeTroop : MonoBehaviour
{
public GameObject troop;
bool place;
public void clicked()
{
GameObject placedTroop = Instantiate(troop, Camera.main.ScreenToWorldPoint(Input.mousePosition), Quaternion.identity);
place = false;
while (!place)
{
placedTroop.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (Input.GetMouseButtonDown(0))
{
place = true;
}
}
}
}
Unity is singlethreaded (mostly), which means that as long as your function is running, no other code will execute. So clicked() is called, it enters that loop, and then nothing else in the engine runs until the loop exits. In this case, that is important because input is not processed either, so GetMouseButtonDown is never going to be true as long as you’re in this loop.
What you can do is have clicked() call a coroutine. Coroutines run like regular code until they reach a line that says “yield return null;” at which point they freeze execution, and then the Unity engine will resume that execution in the next frame. So you can put that line inside your while loop and the game will keep running while you wait for the button to click.
public void clicked() {
StartCoroutine(clickCoroutine());
}
private IEnumerator clickCoroutine() {
GameObject placedTroop = Instantiate(troop, Camera.main.ScreenToWorldPoint(Input.mousePosition), Quaternion.identity);
place = false;
while (!place)
{
placedTroop.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (Input.GetMouseButtonDown(0))
{
place = true;
}
yield return null;
}
}
5 Likes