Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in '2D' started by andalina82, Dec 29, 2019.

  1. andalina82

    andalina82

    Joined:
    Dec 14, 2019
    Posts:
    19
    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:

    Code (CSharp):
    1.  public void ResetSameScene()
    2.     {
    3.         int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
    4.         SceneManager.LoadScene(currentSceneIndex);
    5.         isReload = true;
    6.     }

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

    Code (CSharp):
    1. private void TriggerSparklesVFX()
    2.  
    3.     {
    4.  
    5.              if  (afterDeath.isReload == true & gameObject.name == "SpecialLifeBlock")
    6.              {
    7.              GameObject lifeOneUp = Instantiate(lifeUP, transform.position, transform.rotation);
    8.          
    9.              }
    10. ...
    11. ...
    12. ...
    13.      }
    14.  

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


    Code (CSharp):
    1.   IEnumerator myDelay()
    2.     {
    3.  
    4.         yield return new WaitForSeconds(2.5f);
    5.  
    6.         sceneloader.ResetSameScene();
    7.  
    8.         FindObjectOfType<GameSession>().LoseLife();
    9.  
    10.  
    11.     }
     
    Last edited: Dec 29, 2019
  2. Agent003

    Agent003

    Joined:
    Sep 7, 2018
    Posts:
    55
    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.
     
    andalina82 and MisterSkitz like this.
  3. ZliVuk

    ZliVuk

    Joined:
    Mar 24, 2018
    Posts:
    61
    DoNotDestroy

    Singleton
     
    andalina82 likes this.
  4. andalina82

    andalina82

    Joined:
    Dec 14, 2019
    Posts:
    19
    I was away for a while and didn't see the answers. Thank you guys!