- Home /
Use Singleton Prefab in Button's onClick editor field
Hello,
I have language buttons in the Options panel of my UI. I have a GameManager object, which is a singleton, with the LanguageFileReader script attached ot it. When I press the language icons, it calls LanguageFileReader.SetLanguage(int languageID)
, the languageID variable being set in the editor. When I have a GameManager in the scene and I link it to the buttons, it works fine ; however the way I'd like to do it is to not have it present, be loaded in a previous Loading Scene, and not be destroyed.
I therefore have the GameManager Object as a prefab, with a basic singleton pattern on it :
public static GameManager instance = null;
private void Awake()
{
if (instance == null)
{
instance = this;
}
else if (instance != this)
{
Destroy(gameObject);
}
DontDestroyOnLoad(gameObject);
}
and a script attached to the MainCamera of every Scene :
public class Loader : MonoBehaviour
{
public GameManager gameManager;
void Awake()
{
if (GameManager.instance == null)
{
Instantiate(gameManager);
}
}
}
The GameManager prefab is linked on the Loader script of every scene.
So with this method, the buttons don't do anything because they have no referenced GameManager in the editor, and if I try to pass the prefab it doesn't work either.
Do I have to code the button's behaviours, or am I doing something wrong ?
On a secondary note, my GameManager object is always shown as (clone) in the editor, why is that ?