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 Collects · Dec 31, 2018 at 04:26 PM · delayrestartrestart game

Restart Delay

I have the code below, but it doesn't have a delay even though there should be.

using UnityEngine.SceneManagement; using UnityEngine; using System.Collections;

 public class GameManagerCubic : MonoBehaviour
 {
     bool gameHasEnded = false; 
 
     public float restartDelay = 2f;
 
 
 public void EndGame()
     {
         if (gameHasEnded == false)
         {
             gameHasEnded = true;
             Debug.Log("OVER OVER");
             Invoke("Restart", restartDelay);
         }
     }
 
     void Restart ()
     {
         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
     }
 
     
 
 }
Comment
Add comment · Show 6
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 Hellium · Dec 31, 2018 at 04:39 PM 1
Share

Are you SURE the value of restartDelay is greater than 0 IN THE INSPECTOR?

  public void EndGame()
  {
      if (gameHasEnded == false)
      {
          gameHasEnded = true;
          Debug.Log("Invoking Restart in " + restartDelay + " second(s)");
          Invoke("Restart", restartDelay);
      }
  }
avatar image Collects Hellium · Dec 31, 2018 at 04:44 PM 0
Share

Yes, It is.

avatar image Collects Hellium · Dec 31, 2018 at 04:46 PM 0
Share

I tried using the code you submitted, but It isn't working.

avatar image Hellium Collects · Dec 31, 2018 at 06:29 PM 0
Share

$$anonymous$$y code wasn't supposed to fix anything. It prints the value of the restartDelay variable. What is its value?

Are you sure you don't destroy the gameObject / disable the script before the EndGame is called?

Show more comments
avatar image c_Feng · Dec 31, 2018 at 08:33 PM 1
Share

Sounds like you're destroying the game object the script is attached to after calling "EndGame". $$anonymous$$ake sure this isn't the case:

In Play mode, select the game object with the script attached. Then when "EndGame" gets triggered, make sure it remains in the scene hierarchy.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by FriezKing · Dec 31, 2018 at 07:46 PM

Instead of using the Invoke function, have you thought of using a coroutine?

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.SceneManagement;

  public class GameManagerCubic : MonoBehaviour
  {
      bool gameHasEnded = false; 
  
      public float restartDelay = 2f;
  
  
  public void EndGame()
      {
          if (gameHasEnded == false)
          {
              gameHasEnded = true;
              Debug.Log("OVER OVER");
              Restart();
          }
      }
  
      void Restart ()
      {
                  StartCoroutine("Restart2");
      }
      IEnumerator Restart2() 
      { 
                  yield return new WaitForSeconds(restartDelay);
                  SceneManager.LoadScene(SceneManager.GetActiveScene().name);
      }
  }

I'm no expert, but I'm pretty sure that should fix it! Please tell me if you recieve any errors.

Comment
Add comment · Show 1 · 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 Collects · Dec 31, 2018 at 07:56 PM 1
Share

That didn't work, I don't understand, I'm doing everything right in the inspector and I used the code, but it isn't working.

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

100 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

Related Questions

,Restart Scene after level Completion 0 Answers

Restart Game from Button, not SceneMangement 1 Answer

Current Level Load Again After Level Completion 2 Answers

i have a button to restart the same scene but it dont works (with images) 7 Answers

I bought unity games for reskin. package name how do I change the game name?,reskin 0 Answers


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