While Loop & Bool (infinite loop freeze)

This is probably a really simple question, can’t figure it out because I’m very new so any help would be appreciated

I want to run a while loop, and while my game object is spawned you can click it. But I’m not sure how to execute a while loop where the condition is a Boolean.

As soon as the chest is spawned and the chestSpawned == true, I get frozen in unity as its infinite. In 5 seconds the chestSpawned will return false but unity gets frozen before then so I’m not sure how to fix this.

Here is what I have.

	while (chestSpawned == true)
		{
			Debug.Log ("Click the chest!!!");
		}

If you have it in something like an update function, you don’t need to use a “while” loop. The game automatically runs it once a frame. So try doing if(chestSpawned) Debug.Log (“Click the chest!!!”);


A more difficult but maybe cleaner approach would be to trigger a coroutine that prints every frame until 5 seconds is up, and then kills itself. I doubt that would be needed in this case though.