#Save Manager Question

1 messages · Page 1 of 1 (latest)

heavy sundial
#

I did notice that there were multiple instances of the Save Manager object being loaded in the scene, but I have this on Awake:

#
        if (Instance == null)
        {
            Instance = this;
            DontDestroyOnLoad(this);
        }
        else
        {
            Destroy(this.gameObject);
        }
past fable
#

if you don't return there the rest of the method will run

#

which maybe has your other logic in it?

heavy sundial
#

This is the only other thing running on Awake:

    private void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
            DontDestroyOnLoad(this);
        }
        else
        {
            Destroy(this.gameObject);
            return;
        }

        SceneManager.sceneLoaded += GatherReferences;
    }

But I'll try out the return line

#

Unfortunately it seems like the coroutine is still being repeated

#

Yup, each subsequent load adds a copy of the gameobject

#

@past fable

past fable
#

SceneManager.sceneLoaded += GatherReferences;

#

this is actually your problem

#

you need to add this

#
void OnDestroy() {
  SceneManager.sceneLoaded -= GatherReferences;
}```
#

Otherwise you're never unsubscribing from that event

#

and all of the subscribers will fire every time

#

But also this will only work if they get destroyed properly

#

Gotta figure out why they're not being destroyed

heavy sundial
#

@past fable Darn, thought that would work, but it's still doing the same thing. Here's the full script. Thanks for the help, I'm scratching my head at this. Never worked with a save system before:

public class SaveManager : MonoBehaviour
{
    SaveManager Instance;

    private string SAVE_KEY = "SaveData1";
    public GameObject player;
    public PlayerAmmoCounter playerAmmoCounter;
    public PlayerHealth playerHealth;

    private void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
            DontDestroyOnLoad(this);
        }
        else
        {
            Destroy(gameObject);
            Debug.Log("Destroyed " + gameObject.name);
        }

        SceneManager.sceneLoaded += GatherReferences;
    }

    private void OnDestroy()
    {
        SceneManager.sceneLoaded -= GatherReferences;
    }

    void GatherReferences(Scene scene, LoadSceneMode mode)
    {
        player = GameObject.FindGameObjectWithTag("Player");
        playerAmmoCounter = player.GetComponentInChildren<PlayerAmmoCounter>();
        playerHealth = player.GetComponent<PlayerHealth>();

        Transform checkpoints = GameObject.Find("Checkpoints").transform;
        foreach (Transform checkpoint in checkpoints)
        {
            if (checkpoint.TryGetComponent(out Save_Checkpoint saveCheckpoint))
            {
                saveCheckpoint.saveManager = this;
            }
        }
    }

    public IEnumerator LoadGame()
    {
        if (DataSerializer.TryLoad<SaveData>(SAVE_KEY, out var loadedData))
        {
            SceneManager.LoadScene(loadedData.CurrentScene);
            yield return new WaitForSeconds(0.1f);

            player.transform.position = loadedData.Position;
            playerHealth.playerHealth = loadedData.Health;
            playerAmmoCounter.SetAmmo(loadedData.Ammo);

            Debug.Log("Found save file");
        }
    }
}

#

Minus the Update function because it ended up being too long of a message

past fable
#

Then it'll all work

heavy sundial