Bob5421
Bob5421

Reputation: 9073

C# code which is always run

I have an unity project with multiple scenes.

I switch a lot of time between each scene.

I want to have a C# class which is always run. Is it possible to have a background scene which is always started ?

Thanks

Upvotes: 0

Views: 524

Answers (2)

FaTaLL
FaTaLL

Reputation: 511

In the beginning of your game you can call a function in you awake() function called SceneManager.LoadScene("YourScene", LoadSceneMode.Additive); This will load the scene you wanted and not destroy your other scenes. If you switch between scenes like this you can make one scene always on working.

See more information here;

But if you want only scripts to run on every scene. Use DontDestroyOnLoad function on a gameobject instead. Like @Steffan Venema suggests.

Hope this helps...

Upvotes: 0

Steffan Venema
Steffan Venema

Reputation: 176

You can create a gameobject, attach your C# script and call DontDestroyOnLoad(gameObject) this will keep your gameobject, with its scripts, loaded when switching between scenes.

You can check the Unity Docs here for more information on the DontDestroyOnLoad function

Upvotes: 3

Related Questions