|
I am attempting to make a turret that will only track and shoot at the player if it is with in a certain radius. I found a good script on this site, but when I use it, unity will freeze up when my character enters the trigger area. The code I am using is this: I have my fireball prefab linked to the bulletPrefab, my character is tagged "player", and I am using a spherical collider for the trigger. Any ideas why my game would crash?
(comments are locked)
|
|
The while (target != null) loop has no way of exiting. It runs through the loop infinitely and crashes the program. I think what you're looking for is an IF statement rather than a WHILE statement. IF only iterates once and won't crash your program.,Your
(comments are locked)
|
|
Thommas - your functions "OnTriggerEnter" & "OnTriggerExit" will checked every frame and thus If player inside your Trigger then EVERY FRAME WILL EXECUTED "Fire"-but this function will executed when "while (target != null)"-it will forever in current frame. That's why you look mistake - when you insert two "if" without "while" your code will work right.
(comments are locked)
|

Hmm... For some reason, it didn't like my while (target != null) loop. Reworking it to have an if statement, like in this reference http://unity3d.com/support/documentation/ScriptReference/Time-time.html fixed it. I still don't know why it crashed, but... it works!