I'm having trouble with this script. When I collide with the trigger I want to destroy my character and spawn a different one. Most of the time it spawns 2 characters in the same place at the same time, other times it just spawns one character. I have absolutely no idea why it is doing this. Is there an easy fix to this problem?
I believe its being caused by the 'StartCoroutines' that you have.
Try replacing them with a:
The '0' in the Invoke is if you would like a time delay to trigger the function, so if you have a: Invoke("spawn", 10); - then your spawn function will be called in 10 seconds.
Hope that helps.
answered Apr 05 '11 at 01:38 PM
The basic problem seems to be that Instantiating the new boat is reTriggering the trigger box. In other words, new spawns can cause OnTriggerEnter. You've got the "flag" spawnBoat to prevent that.
The rest of the problem is likely that coroutines can run in any order. Sometimes it sets dockboat=false, spawns, resets it to true, then reruns OnTriggerEnter for the 2nd boat, with spawnBoat back to true.
Adding a Wait or yield command to TrueAgain would cause everything else to run first (with a false spawnBoat.) Likewise adding a delay to Invoke would do the same thing.
A comment: I don't think you could move the lines from spawn into OnTriggerEnter.
answered Apr 06 '11 at 08:14 PM