Hi everyone,
I’m currently wondering what is the best way to organize code, methods and messages between all objects in my unity project.
1° Here is what I want to achieve:
When a player fall in a ditch, the screen must fade in black player should respawn somewhere, screen must fade out.
2° Here is how it’s actually working:
A big “end of the world” collider has an “end of the world” script attached to it. This script detects the collision with player and call the public Respawn() method of the Player script.
The Respawn method just starts a coroutine called RespawnCoroutine
The RespawnCoroutine starts a coroutine from the main camera to fade in black (meaning I also have a reference to the camera in the player Script).
It then changes the player’s position to where it should go, then yield for a second then starts the fade out coroutine of the camera.
The fade in and out coroutines on the camera are changing the alpha value of the color of an image of the canvas.
3° Conclusion:
Just to respawn a player, I have code written in three different scripts and use 4 objects…
4° QuestionS:
- Is it a common pattern to create a public method that just starts a private coroutine so you don’t have to use StartCoroutine() in other scripts? Because I find it confusing to create a public coroutine and Start it from elswhere.
- Would it be easier, cleaner and/or more performant to use delegates and events? Ex: notify camera onPlayerDies so it fade in ?
- Would it be easier, cleaner and/or more performant to reload the scene with the player placed on last checkpoint? Then I save the last checkpoint in some GameManager class and this class call/notify the camera to fade in or out when level is loaded or ended? So when player fall, level is reloaded and I use only levelmanager and camera.
- Is there a simple camera trick to fade in or out instead of adding an image in my canvas? can I add this image on my camera for example ? (it would at least makes more sense to me than having a big black screen on my UI)
Thanks in advance for those who want to think about it even if there isn’t any code here to make it more easy to understand 
