- Home /
Android coroutine loop ruins game
so I am making a game where i am trying to draw a line based on the users input. for that i am using a coroutine, with a while loop`function draw() :IEnumerator{ while(Input.GetMouseButton(0)){ Debug.Log("startPosss="+startPos);
endPos = Input.mousePosition;
endPos.z = 20;
endPos = Camera.main.ScreenToWorldPoint(endPos);
platform.localScale.x = endPos.x-startPos.x;
Debug.Log("endpos="+startPos);
//brick.localRotation.z = endPos.y - startPos.y;
if(startPos.y<endPos.y)
{
Debug.Log("lll");
distance = Mathf.Sqrt(Mathf.Pow((startPos.x-endPos.x),2)+Mathf.Pow((startPos.y-endPos.y),2));
rotationAngle = Mathf.Rad2Deg * Mathf.Asin((Mathf.Abs(endPos.y-startPos.y))/distance);
if(startPos.x>endPos.x){
rotationAngle = 180-rotationAngle;
}
//Debug.Log("distance="+distance);
//Debug.Log("rotationAngle="+rotationAngle);
for(var j = 0; j<GameObject.FindGameObjectsWithTag("Platform").Length;j++){
Destroy(GameObject.FindGameObjectsWithTag("Platform")[j]);
}
Instantiate(platform, Vector3((endPos.x-startPos.x)/2+startPos.x,(endPos.y-startPos.y)/2+startPos.y,0.0), Quaternion.Euler(0,0,rotationAngle));
}
else{
distance = Mathf.Sqrt(Mathf.Pow((startPos.x-endPos.x),2)+Mathf.Pow((startPos.y-endPos.y),2));
rotationAngle = Mathf.Rad2Deg * Mathf.Asin((Mathf.Abs(endPos.y-startPos.y))/distance);
if(startPos.x<endPos.x){
rotationAngle= 180-rotationAngle;
}
//Debug.Log("distance="+distance);
//Debug.Log("rotationAngle="+rotationAngle);
for(var i = 0; i<GameObject.FindGameObjectsWithTag("Platform").Length;i++){
Destroy(GameObject.FindGameObjectsWithTag("Platform")[i]);
}
Instantiate(platform, Vector3((endPos.x-startPos.x)/2+startPos.x,(endPos.y-startPos.y)/2+startPos.y,0.0), Quaternion.Euler(0,0,rotationAngle));
} yield WaitForSeconds(0.01); }
}` . In my unity editor everything is fine, yet on a android device, once i try to draw, it goes straight to game over scene, and when ever i try to click retry, it goes back for a second and then back to game over scene.
I solved the problem. Yet i encountered another. when i draw a line, it appears a bit below where i started. can anyone help me with this?
Answer by Jeff-Kesselman · May 13, 2014 at 01:54 PM
Why arent you just doing this on Update rather then trying to run a whole separate threaded loop?
because i want the person to be able to see his line being drawn. therefore i am using this loop. yet whenever i use the loop in the update function, it crashed unity
i don't know if you understood, i ran the function "draw()" in a coroutine in the Update function
Your answer
![](https://koobas.hobune.stream/wayback/20220613144936im_/https://answers.unity.com/themes/thub/images/avi.jpg)