[Solved] Trying not to instantiate an object after reload scene

I want to Instantiate an object (life) by hitting a block when the scene loads the first time, but when the scene resets or reloads (in case of after death), I dont’t want to instantiate the object again.

I tried to play with a bool, if the scene “reloads” = true, then don’t do that, but the scene reloads as a whole as it is supposed to be when it first runs. Or maybe I have the bool in the wrong place.

I would appreciate any help, and if the code is confusing I can explain.

This is the ResetSameScene method in the SceneLoader script:

 public void ResetSameScene()
    {
        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
        SceneManager.LoadScene(currentSceneIndex);
        isReload = true;
    }

This is the method in the Block script where I want to instantiate the object:

private void TriggerSparklesVFX()

    {
 
             if  (afterDeath.isReload == true & gameObject.name == "SpecialLifeBlock")
             {
             GameObject lifeOneUp = Instantiate(lifeUP, transform.position, transform.rotation);
         
             }
...
...
...
     }

and this is where the ResetSameScene gets triggered with the block collider:

  IEnumerator myDelay()
    {
  
        yield return new WaitForSeconds(2.5f);

        sceneloader.ResetSameScene();
 
        FindObjectOfType<GameSession>().LoseLife();
 

    }

If i were you, i would make an empty script with a static bool (false by default) if the scene gets reset the bool change to true, then add a check condition before instantiating.

  • Don’t attach the bool script to any game object.
2 Likes

DoNotDestroy

Singleton

1 Like

I was away for a while and didn’t see the answers. Thank you guys!