- Home /
Error in script to make a monster shoot against the player
I have done a function to use in a script for one of my monsters (on my 3d plattaform game) and I have a problem with it, when I test the game, it crashes with unity and I have to terminate the process through Ctrl+alt+del. Do you have any idea what is going wrong?
Here you have the code (I have a rigidbody on unity too to use it as bullet, and its passed through the editor to the script):
var projectile : Rigidbody;
function Shoot () { while(true){ // Instantiate the projectile at the position and rotation of this transform var clone : Rigidbody; clone = Instantiate(projectile, transform.position, transform.rotation);
// Give the cloned object an initial velocity along the current
// object's Z axis
clone.velocity = transform.TransformDirection (Vector3.forward * 10);
}
// yield for one frame yield;
}
Answer by Jessy · May 06, 2011 at 06:19 PM
You put yield outside of your infinite loop. Put in before the curly brace instead of after. If you line up your curly braces better, you'll have a better chance of catching mistakes yourself.
Thanks, now Unity don't crash, but there is a very weird effect on my script: Here you have a link on youtube about the effect, I have just recorded it : http://www.youtube.com/watch?v=b9xm_UZRqX0
I don't have a clue what is going right or wrong in that movie. Why didn't you narrate it?
Your answer
![](https://koobas.hobune.stream/wayback/20220613132232im_/https://answers.unity.com/themes/thub/images/avi.jpg)