UPDATE
After a little more digging found out you cant easily use dont destroy on load on an object which has public editor references to other objects.
So decided to move my score values into another gameobject (make that object, dont destroy on load) and then read write to that before and after loading of the scene with my game manager object using the Find function.
Best way i can see how to do it unless anyone has any other ideas.
Appears to be working for now though.
ORIGINAL POST
Hi everyone
Need a lil help with singleton instance of an object and dontdestroyonload.
I have some code in the Awake function which should be setting my gamemanager to a singleton instance and then setting it to not be destroyed when the scene is reloaded.
What appears to be happening however is my instance referenced object is not being recognised and is being destroyed on restart.
This leaves a new gamemanager object in its place which is then missing references.
Im assuming its all down to the singleton referencing as when i just set the code dont destroy on load i end up with two game manager objects.
one with all my references and one without.
Anyone help me out with what i may be missing
public static GameController instanceRef;
private float currentScore ;
private void Awake()
{
if (instanceRef == null) {
instanceRef = this;
DontDestroyOnLoad (gameObject);
}
else if (instanceRef != this)
Destroy (gameObject);
}
void Restart()
{
SceneManager.LoadScene (SceneManager.GetActiveScene ().name);
}