- Home /
Read Singleton across scenes?
I'm new to using singletons and I head using one for my inventory was a correct use for one since you only want one. I currently have three scenes outside, in one building, and a scene where I test new features/items. I currently have the inventory script on a game manager that's in all three scenes because the UI needs it but unity (Rightfully) doesn't like that I have this singleton in multiple scenes. Is there a way I can access that singleton in all my scenes.
Answer by The-Peaceful · May 05, 2021 at 10:27 AM
You can use Unity's 'DontDestroyOnLoad' method like this:
private void Awake()
{
if ( Instance != null && Instance != this )
{
Destroy(gameObject);
return;
}
Instance = this;
DontDestroyOnLoad(gameObject);
}
Pretty much the same old Singelton implementation, with the tiny change that this gameObject won't be destroyed if a new scene is loaded. :D
https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
Your answer
![](https://koobas.hobune.stream/wayback/20220613033320im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how to use a random number generated in one scene into another 1 Answer
How can you copy all of the colliders in a scene 1 Answer
Creating Sprite Sheets for a 2D game Unity 0 Answers
UI Canvas EventSystem Scripts not working 1 Answer
How do you change gravity with a press of a button? Unity 2D 2 Answers