Organizing code in Unity, what would you do?

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 :slight_smile:

I’m still going through the Unity course, and this is the first time I’ve even heard of coroutines, so keep that in mind when reading my response:

  • From what I’ve seen, the most widely used method of calling a coroutine is to use StartCoroutine(), and to do so from wherever you need it. Calling it from a public method seems to be more of an anti-pattern.
  • To me, this seems like what you want to go for.
  • If performance is a concern, I’d think you’d want to avoid this unless you have scripted events that need to be reset so that they fire correctly.
  • Actually, yes! Exercising my Google-fu, I found this Unity Answers topic that describes exactly what you are talking about! You still need to drop a GUI object in, but this way fade in and out are controlled from a script on the camera.

If it’s possible, I would love to see your code! Do you have a git repo or anything that you could share and we could see better what you are doing?

EDIT: I looked around at a few different sources for coroutine. This had the best explanation I found. So this is were the bulk of my coroutine knowledge comes from.

1 Like

Thank you!

Finally I didn’t need that respawning anymore on that particular project :confused:
But I have used delegates and events, I did call coroutines directly with StartCoroutine, and did not reaload the scene.
As for the camera fade, I did read that topic too but it seems I wasn’t focused enough to see my solution in there, it is so simple, I like it :slight_smile:

I do have a git account but no repo for that project.

Thanks for your answer :slight_smile: