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. Dismiss Notice

delay spawn

Discussion in '2D' started by elankish1, Feb 11, 2021.

  1. elankish1

    elankish1

    Joined:
    Nov 21, 2020
    Posts:
    18
    I made a particle system made to play when the player dies. But when the player dies the scene instantly reloads giving the player no time to see the particle. Is there a way to fix that.

    The code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class playerdeath : MonoBehaviour {
    7.  
    8.     public int Respawn;
    9.     public ParticleSystem explosion;
    10.     public Transform particlespawn;
    11.    
    12.     void OnCollisionEnter2D(Collision2D collision) {
    13.         if (collision.gameObject.CompareTag("enemy")) {
    14.             Destroy(gameObject);
    15.             Instantiate(explosion, particlespawn.position, transform.rotation);
    16.         }
    17.     }
    18.  
    19.    
    20. }
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,333
  3. elankish1

    elankish1

    Joined:
    Nov 21, 2020
    Posts:
    18
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,343
    Perhaps if you do actually want help, it'd be useful to state what the error is. Helping you requires that you put some effort into your posts and provide useful information otherwise you're not going to get the help you want.
     
    elankish1 likes this.
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,343
    Referring to your original post, nothing in it whatsoever relates to scene loading so I'm not sure of the purpose of posting that script. The reply above means doing the scene loading by calling a function that does it using the Invoke method that has an argument that allows you to specify when it's called i.e. get the scene to load after several seconds.
     
    elankish1 likes this.
  6. elankish1

    elankish1

    Joined:
    Nov 21, 2020
    Posts:
    18
    it says cannot convert int to string
     
  7. elankish1

    elankish1

    Joined:
    Nov 21, 2020
    Posts:
    18
    I also used the wrong version of the code
    here is the one
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class playerdeath : MonoBehaviour {
    7.  
    8.     public int Respawn;
    9.     public ParticleSystem explosion;
    10.     public Transform particlespawn;
    11.    
    12.     void OnCollisionEnter2D(Collision2D collision) {
    13.         if (collision.gameObject.CompareTag("enemy")) {
    14.             Destroy(gameObject);
    15.             Instantiate(explosion, particlespawn.position, transform.rotation);
    16.             Invoke(Respawn, 2f);
    17.             SceneManager.LoadScene(Respawn);
    18.         }
    19.     }
    20.  
    21.    
    22. }
     
  8. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,333
    You arent using the Invoke method properly. Remove the Scenemanager.LoadScene(Respawn) line.

    Create a new function within that script.

    void LoadSceneAgain()
    {

    string sceneName = SceneManager.GetActiveScene().name;
    SceneManager.LoadScene(sceneName,LoadSceneMode.Single);
    }

    Then, use Invoke right where you have it but to use it properly, you need to fill in the parameters correctly.

    Invoke("LoadSceneAgain", 2f);
     
    MelvMay likes this.
  9. elankish1

    elankish1

    Joined:
    Nov 21, 2020
    Posts:
    18
    so like this

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    public class playerdeath : MonoBehaviour {
    public int Respawn;
    public ParticleSystem explosion;
    public Transform particlespawn;

    void OnCollisionEnter2D(Collision2D collision) {
    if (collision.gameObject.CompareTag("enemy")) {
    Destroy(gameObject);
    Instantiate(explosion, particlespawn.position, transform.rotation);
    Invoke("LoadSceneAgain", 2f);
    LoadSceneAgain();
    }
    }
    void LoadSceneAgain()
    {
    string sceneName = SceneManager.GetActiveScene().name;
    SceneManager.LoadScene(sceneName, LoadSceneMode.Single);
    }
    }
     
  10. elankish1

    elankish1

    Joined:
    Nov 21, 2020
    Posts:
    18
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class playerdeath : MonoBehaviour {
    7.  
    8.     public int Respawn;
    9.     public ParticleSystem explosion;
    10.     public Transform particlespawn;
    11.    
    12.     void OnCollisionEnter2D(Collision2D collision) {
    13.         if (collision.gameObject.CompareTag("enemy")) {
    14.             Destroy(gameObject);
    15.             Instantiate(explosion, particlespawn.position, transform.rotation);
    16.             Invoke("LoadSceneAgain", 2f);
    17.             LoadSceneAgain();
    18.         }
    19.     }
    20.  
    21.     void LoadSceneAgain()
    22.     {
    23.  
    24.         string sceneName = SceneManager.GetActiveScene().name;
    25.         SceneManager.LoadScene(sceneName, LoadSceneMode.Single);
    26.     }
    27.  
    28.  
    29. }
    30.  
     
  11. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,333
    Remove the LoadSceneAgain on line 17. That will cause it to call immediately, which is the problem you are having. The Invoke is a method to call another method, but delayed. So you are already calling it within Invoke therefore no need to call it right after.
     
  12. elankish1

    elankish1

    Joined:
    Nov 21, 2020
    Posts:
    18
    ok but If i remove that command the player wont respawn at all
     
  13. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,333
    Yes he will. The whole scene will restart in 2 seconds instead of immediately. You arent understanding how Invoke works. Invoke calls the function, but delayed instead of calling it immediately.

    Here is what your code says:
    Invoke: Restart scene in 2 seconds by INVOKING the function LoadSceneAgain
    IMMEDIATELY RESTART SCENE when you call LoadSceneAgain normally.
     
  14. elankish1

    elankish1

    Joined:
    Nov 21, 2020
    Posts:
    18
    buddy it does not im going to end it at that I have an entire video 30 seconds long of evidence if only I could send it
     
  15. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    You can. Please do, because what @Cornysam is suggesting above should be working.
     
  16. elankish1

    elankish1

    Joined:
    Nov 21, 2020
    Posts:
    18
     
  17. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Ah, I see now.
    Because the GameObject is destroyed, the "LoadSceneAgain" method doesn't exist anymore by the time Invoke gets to call it.

    Instead of destroying the GameObject, try disabling it.
     
  18. elankish1

    elankish1

    Joined:
    Nov 21, 2020
    Posts:
    18
    Thank you so much but how do I do that im very new to gamedev code wise
     
  19. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Replace
    Destroy(gameObject)
    with
    gameObject.SetActive(false)
     
  20. elankish1

    elankish1

    Joined:
    Nov 21, 2020
    Posts:
    18
    thank you