0

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.

  1. 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);
             }
         }
     }
    
  2. 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);
         }
    }
    
FrozenAra
  • 509
  • 2
  • 14
  • I'm not sure how the ScreenPersists class is related to the loading of coins. It looks like you are checking if you are on the starting scene each frame. If you are not currently in the satarting scene you are destroying the ScreenPersist game object. – Anton Aug 12 '21 at 09:04
  • I have the coins in a pickups game object nested as a child of the screenPersist game object – raymend le Roux Aug 12 '21 at 09:08
  • I have tried to do it without the screenPersist class, but then every time the player dies and the scene restarts at the checkpoint the coins that you already picked up get spawned again. – raymend le Roux Aug 12 '21 at 09:21
  • If you want to respawn player at the last checkpoint then do just that. There is no need to reload the whole scene. Keep track of the last checkpoint, and move player there once they die. – Anton Aug 12 '21 at 09:25
  • Thanks I have Solved the problem, I am still very new to Unity and C# So I appreciate the help. – raymend le Roux Aug 12 '21 at 10:16

2 Answers2

0

If your coin pickups are children of the game oject to which ScreenPersists is attached to then you are destroying that obejct.

In update method (which is called every frame) you are checking if you are on the starting scene. This object will persist to the next scene. After the scene is loaded it will check if it is still in the starting scene. If not then it will destroy itself taking its children with it.

I'm not sure why are you destroying the object if you want it to persist. So the solutin is to remove the stuff in the update method.

Anton
  • 131
  • 6
0

You currently have no control over which instance of the ScreenPersist GameObject gets destroyed, if you simply check the count and delete. The correct way to maintain a single instance of a DontDestroyOnLoad() object is like this:

 public static ScreenPersist m_screenPersist;
private void Awake()
{
    DontDestroyOnLoad(gameObject);
    if (m_screenPersist == null) m_screenPersist = this;
    else Destroy(gameObject);
}

Ideally, you wouldnt want to have the coin pickups as a child of the ScreenPersist GameObject. Better to have a game manager that takes care of it more elegantly, and finally writes any data that you need to persist, into the ScreenPersist class, or into the same GameManager class which can function as a DontDestroyOnLoad GameObject

varunkaustubh
  • 301
  • 1
  • 7