Loading The Saved Scene lecture

problem with Ienumerator start() function.


The most common cause for this is forgetting to make the hasSpawned a static variable. If it’s not static, then when the scene reloads, it doesn’t know that the PeristentObjectsClone has been spawned, so it loads another. Each load of the prefab causes the SavingWrapper to LoadLastScene.

namespace RPG.Core
{
    public class PeristentObjectSpawner : MonoBehaviour
    {
        [SerializeField] private GameObject persistentObjectPrefab;

        private static bool hasSpawned = false;

        private void Awake()
        {
            if (hasSpawned) return;

            SpawnPersistentObjects();

            hasSpawned = true;
        }

        private void SpawnPersistentObjects()
        {
            var persistentObject = Instantiate(persistentObjectPrefab);
            DontDestroyOnLoad(persistentObject);
        }
    }
}

thank you.
it fix it.
I forgot :
“hasSpawned = true;”

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms