Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jjoohhnnyy13 · Mar 16, 2019 at 10:16 AM · unity 5destroy objectexplosionload scene

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jjoohhnnyy13 · Mar 16, 2019 at 10:48 AM 0
Share

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

avatar image meMUmar jjoohhnnyy13 · Mar 16, 2019 at 12:14 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

194 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges