I've made a game with Unity and I want to execute a C# script that runs even the game was closed.
I searched a lot but didn't find anything.
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It only takes a minute to sign up.Sign up to join this community
I have not seen a way to do this in Unity, which makes sense. Unity is not developed to run as a service. However, it has something close. Look up Unity Application.runInBackground. You can couple this with the OnApplicationFocus event to determine the state. Normally a game just pauses in the background.
But for clarity, you are specifically asking about running code after the program has been closed. This is typically not done with Unity, but what most applications do, is pair the installation with a service that will run in the background. In general, it is considered a bad practice for user interest.
When user's discover your application is eating battery, bandwidth and processing cycles when not in use, they will assume it is for spying on them.
What more places might choose to do, is just execute something at the start or end of the programs execution. To capture the ending event, even when not done from a close button, use OnApplicationQuit. Then, using a timed event to announce things to a user, such as this one for Android.
Events are how many apps give notifications on mobile when not running. On exiting, the games will usually understand based on timing, when an event will occur. Such as building X will complete in 3 minutes and 41 seconds, or the next online game cycle starts at midnight.