Can anyone please explain to me why the Yield Function Freezes My Game? I’m trying to Respawn a Player Upon Death. Naturally we want to WaitforSeconds while the Player’s Death Animation is Played and Clamped Until Timer is Up and we continue to Respawn the Player. In the following Script, if I change the Respawn() to void, and comment out the yield function, the Respawn Works Instantaly (No Player Death Animation Played). If I set this Respawn() as IEnumerator with yield return new WaitForSeconds(3); it still works, only the game Freezes entirely for 3 seconds then respawns the player. Doesn’t make much sense to me… please help!
Basically call RPC to StartCoroutine( RespawnSequence() ). I never tried calling photonView.RPC() on a coroutine function so I can’t tell why exactly is that happening.
Thanks, I tried it but nope doesn’t work… I get the same results. Here is Screenshot for better visual of whats happening when this Script Runs. I don’t even think this is a Networking Issue at all, as the Respawn Works Perfectly Fine. The main issue is Client B as shown below just Freezes for 3 Seconds instead of showing the Player’s Death Animation. Oddly enough I can see the Player Die on Client A (Death Animation Plays Back but for some odd reason Plays returns to Idle State then Respawns, but that’s another issue I think). Main problem is Freezing due to the Yield Function. Once I comment out Yield and Remove Couroutine everything works fine, no freezing at all…
Well the weird thing is, as mentioned if I remove the Couroutine, the respawn functions that are inside the photonView.isMine as you point out above works just fine. There is no freezing at all if I remove the Yield. So for me that indicates there is no problem with the re-spawn functions, but maybe i’m looking at this the wrong way lol, and maybe it is some kind of Networking issues I don’t understand. I wish I would get some console errors, but I don’t… = (
Sorry I only have some debugging ideas:
What happens if you yield return WaitForSeconds(0)?
Are you certain the game is frozen? If client A is moving around does client B see that?
Perhaps there is a problem with playing the animation on the local client.
you could also try 3x yield return WaitForSeconds(1) with debug lines between
You’re still not really providing enough information. What does PlayerAnimation.Dead() do? Is A the one dying or is B the one dying? In those instances, who is invoking Respawn? When you say “freeze” what do you mean? Unity’s RPC does not support coroutines - have you checked Photon documentation to see if it is supported there? Have you checked the “frozen” client log for exceptions that would halt execution? IE - one red flag to me would be looking for a CharacterController on client instances that don’t own the player object being killed. If that failed then Dead() would never be called and your “frozen” state would just be the time spent yielding in the caller’s RPC method before they sent the actual Revive RPC.
Calls Dead Animation State, and Plays Player Animation
Is A the one dying or is B the one dying?
In the example screenshot above, B is dying and is laying on the ground.
In those instances, who is invoking Respawn?
Client B is invoking Respawn since the Respawn is scripted into the Player script that checks if HP is equal to 0
When you say “freeze” what do you mean?
Client B remains uncontrollable, and all objects in the scene just stops (example other players, or any visable dynamic objects in Client B’s view will remain frozen for 3 seconds as the Coroutine completes then Respawns Player (Client B).
Unity’s RPC does not support coroutines - have you checked Photon documentation to see if it is supported there?
According to this Photon Support thread I found it does seem like Coroutine is supported in RPC.
Have you checked the “frozen” client log for exceptions that would halt execution?
Keep in mind also that if you disable (.SetActive(false)) the actual game object upon which this respawn script is running, the Coroutine will stop… that would not cause a freeze, but it is just something to keep in mind.(Disclaimer: I did not read your code close enough to see if that might be the case.)