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
Use Coroutine or Invoke to load scene after a specific time interval.
For Coroutine check this : Unity - Scripting API: MonoBehaviour.StartCoroutine
And for Invoke function check out these docs : Unity - Scripting API: MonoBehaviour.Invoke
Hope this will help with your problem.
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
}