Writing simple mover, Unity engine crashes?
so i'm trying to make a gameObject > Quad to move left and right in an infinite loop
public class bossMover : MonoBehaviour
{
public float speed2;
void Start()
{
bossmover();
}
void bossmover()
{
while (true)
{
GetComponent<Rigidbody>().velocity = transform.right*-1 * speed2;
if (GetComponent<Rigidbody>().position.x < -1.90f)
{
GetComponent<Rigidbody>().velocity = transform.right * speed2;
}
if (GetComponent<Rigidbody>().position.x > 2.11f)
{
GetComponent<Rigidbody>().velocity = transform.right * speed2;
}
}
}
}
Unity Engine crashes when i run the scene(speed2 value 5). Everything works just fine in the game instead of this script.
Answer by Toothless_ · Feb 11, 2017 at 10:54 PM
the while(true)
will cause an infinite loop in the start function , unity does not like this so crashes. What you could so to stop the crashing would be to remove the while(true)
and move the function call to Update()
Your answer
![](https://koobas.hobune.stream/wayback/20220612110900im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Object moves through the position of another object 0 Answers
2D touch - Why is this movement so jittery? 1 Answer
Shader Graph with Tilemaps (and Sprites) 1 Answer
Build crashes but not editor, cannot narrow down cause 0 Answers
Unity 2019 Stuck on "Compiling Assembly Definition Files Scripts" 0 Answers