- Home /
Game not running after script writing
Hey guys how's it going? I wrote a script for a gameobject which is supposed to shoot balls at a certain pace, the console doesn't flash any errors but when I try to run the game the whole engine freezes and it stops responding. Anyone knows what might have caused this? Maybe my laptop needs to cool down or something... anws here's the script: #pragma strict var ball : Rigidbody; var speed : float = 40.0; var canShoot : boolean; var ballNumb : int = 0; function Start () { canShoot = true; }
function Update () {
if(canShoot){
do{
StartCoroutine("Launcher");}
while(ballNumb >= 1);
}
}
function Launcher (){
yield new WaitForSeconds(1);
var newBall : Rigidbody = Instantiate(ball, transform.position, transform.rotation);
newBall.velocity = transform.forward * speed;
ballNumb ++;
yield new WaitForSeconds(0.5);
Destroy(newBall.gameObject);}
At the console window, perhaps you have "Error Pause" checked.
Answer by Philipp · Feb 19, 2013 at 02:18 PM
In Update, you have a loop that starts a co-routine and then loops if ballNumb is bigger than one. Your co-routine waits for a second, then increases ballNumb. From this point onward, ballNumb will always be >= 1, and the next time Update is called, the loop in Update will launch an infinite number of co-routines and never return, causing Unity to hang.
I thought the do-while command was supposed to yield the ball shooting until the current one was shot then destroyed then shoots another 1, any idea how to fix it?
Answer by Professor Snake · Feb 19, 2013 at 05:04 PM
Set canshoot to false at the beginning of launcher and back to true after the waiting. You are starting that coroutine at every frame the way it is.
Yup it worked 10x now onward for the damage scripting