I can’t figure out why I am freezing unity with my while loop. I am trying to check the sate of a boolean variable in another script called “IsObjectThere”. If that Boolean variable is not true and if the mouse button is up then I it to remain in the while loop until the canSpawn variable is true. Once its true I want to break the while loop.
If I run my code without the while loop it runs fine. As soon as I enter the while loop it freezes unity. Below is the part of my code I am dealing with.
var objs : GameObject[];
var respawn : GameObject;
var IsObjectThere : isObjectThere;
var waiting : boolean;
private var obj : GameObject;
function Start()
{
if (respawn==null)
respawn = GameObject.FindWithTag ("Respawn");
}
function Update()
{
if (Input.GetKeyDown (KeyCode.Mouse0))
{
obj = Instantiate(objs[(Random.Range(0, objs.Length))], respawn.transform.position, respawn.transform.rotation);
obj.transform.parent = Camera.main.transform;
IsObjectThere = obj.GetComponent(isObjectThere);
}
if (Input.GetKeyUp (KeyCode.Mouse0)&& IsObjectThere.canSpawn)
{
DropObject();
}
if(Input.GetKeyUp (KeyCode.Mouse0) && (!IsObjectThere.canSpawn))
{
waiting = true;
while(waiting)
{
if (IsObjectThere.canSpawn)
{
waiting = false;
}
}
}
}
function DropObject ()
{
obj.transform.parent = null;
obj.GetComponent(Rotate).enabled = false;
obj.gameObject.tag = "placedObject";
Debug.Log (IsObjectThere.canSpawn);
}