#Save Manager Question
1 messages · Page 1 of 1 (latest)
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);
}
See if this helps:
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(this);
}
else
{
Destroy(this.gameObject);
return;
}```
if you don't return there the rest of the method will run
which maybe has your other logic in it?
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
Ah see
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
@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
Instance needs to be static
Then it'll all work
That fixed it! Man, I can't believe I missed that. Guess my eyes were more tired than normal today lol