- Home /
I need help (go to the next scene after a few seconds after creating the explosion)
First:, I enjoy modeling, not programming. My programming looks like this ctrl + c and ctrl + v and try, mistake. But I can not quite say that when I read the code, so that I would not make sense. Problem: I need to create an explosion when two objects collide. So that after a few seconds (so she could create an explosion), the game switched to the next scene. Code: public class DestroyByContact2 : MonoBehaviour {
public GameObject explosion;
void OnCollisionEnter()
{
GameObject expl = Instantiate(explosion, transform.position, Quaternion.identity) as GameObject;
Destroy(gameObject);
Destroy(expl, 2); // (expl, delete the explosion after... seconds)
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
I've already tried a few of my ideas, but they didn't work with me
Answer by Hellium · Mar 16, 2019 at 03:17 PM
First of all, create a C# script called SceneManager.
using UnityEngine;
using System.Collections;
public class SceneManager : MonoBehaviour
{
private static bool destroyed = false;
private static object lockObject = new object();
private static SceneManager instance;
public static SceneManager Instance
{
get
{
if (destroyed)
{
Debug.LogWarning("[Singleton] Instance '" + typeof(SceneManager) +
"' already destroyed. Returning null.");
return null;
}
lock (lockObject)
{
if (instance == null)
{
// Search for existing instance.
instance = (SceneManager)FindObjectOfType(typeof(SceneManager));
// Create new instance if one doesn't already exist.
if (instance == null)
{
// Need to create a new GameObject to attach the singleton to.
var singletonObject = new GameObject();
instance = singletonObject.AddComponent<SceneManager>();
singletonObject.name = typeof(SceneManager).ToString() + " (Singleton)";
// Make instance persistent.
DontDestroyOnLoad(singletonObject);
}
}
return instance;
}
}
}
public static UnityEngine.SceneManagement.Scene GetActiveScene()
{
return UnityEngine.SceneManagement.SceneManager.GetActiveScene();
}
public static void LoadNextScene( float delay = 0 )
{
LoadScene( GetActiveScene().buildIndex + 1, delay );
}
public static void LoadScene( int buildIndex, float delay )
{
Instance.StartCoroutine( Instance.LoadSceneAfterDelay(buildIndex, delay) );
}
public static void LoadScene( string sceneName, float delay )
{
Instance.StartCoroutine( Instance.LoadSceneAfterDelay( sceneName, delay ) );
}
private IEnumerator LoadSceneAfterDelay( int buildIndex, float delay )
{
if( delay > 0 )
yield return new WaitForSeconds(delay);
UnityEngine.SceneManagement.SceneManager.LoadScene( buildIndex );
yield break;
}
private IEnumerator LoadSceneAfterDelay( string sceneName, float delay )
{
if( delay > 0 )
yield return new WaitForSeconds(delay);
UnityEngine.SceneManagement.SceneManager.LoadScene( sceneName );
yield break;
}
private void OnApplicationQuit()
{
destroyed = true;
}
private void OnDestroy()
{
destroyed = true;
}
}
Then, in your script
// Remove `using UnityEngine.SceneManagement;` at the top of your file
void OnCollisionEnter( Collision collision )
{
GameObject expl = Instantiate(explosion, transform.position, Quaternion.identity) as GameObject;
Destroy(gameObject);
Destroy(expl, 2); // (expl, delete the explosion after... seconds)
SceneManager.Instance.LoadNextScene( 2 ); // Change the delay if you want
}
Answer by meMUmar · Mar 16, 2019 at 10:31 AM
Use Coroutine or Invoke to load scene after a specific time interval. For Coroutine check this : https://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html
And for Invoke function check out these docs : https://docs.unity3d.com/ScriptReference/MonoBehaviour.Invoke.html
Hope this will help with your problem.
yes, I tried it but none of my experiments worked for me. I don't know how to use it properly to make it work
Here is an example i have:
public void LevelFailed() {
StartCoroutine(WaitForGameOver());
}
IEnumerator WaitForGameOver() {
yield return new WaitForSeconds(3f);
levelFailedPanel.SetActive(true);
}
I called a cooroutine in function and in coroutine after a specific time interval i changed some values.
Your answer
Follow this Question
Related Questions
LoadSceneOnClicks.LoadAsynchronously(int)': not all code paths return a value 1 Answer
How do I destroy player when colliding with looping particle system (Explosion) 0 Answers
How to delete prefabs without warning popping up? 0 Answers
Destroy the object i am looking UNITY 3D 1 Answer
Being in scene1 I want to load scene2, but also to disable scene1. How can I do that correctly? 1 Answer