I am creating a 2D platformer game with coin pickups, everything works fine, but when you proceed to the next level the coin pickups of that level won't load correctly.
I have a screen Persist game object with a screenPersist script, then I have my pickups (coins), with a coinPickup script.
The only problem is when you proceed to the next level the coins of that level won't load. the score and everything else works fine.
Here are examples of my scipts.
screenPesist script.
public class ScreenPersist : MonoBehaviour { int startingSceneIndex; private void Awake() { int numScenePersist = FindObjectsOfType<ScreenPersist>().Length; if (numScenePersist > 1) { Destroy(gameObject); } else { DontDestroyOnLoad(gameObject); } } // Start is called before the first frame update void Start() { startingSceneIndex = SceneManager.GetActiveScene().buildIndex; } // Update is called once per frame void Update() { int currentScenIndex = SceneManager.GetActiveScene().buildIndex; if (currentScenIndex != startingSceneIndex) { Destroy(gameObject); } } }
Coin pickup Script.
public class CoinPickup : MonoBehaviour { [SerializeField] AudioClip coinPickupSFX; [SerializeField] int piontsForCoinPickup; private void OnTriggerEnter2D(Collider2D collision) { FindObjectOfType<GameSesion>().addToScore(piontsForCoinPickup); AudioSource.PlayClipAtPoint(coinPickupSFX, Camera.main.transform.position); Destroy(gameObject); } }