Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
1
Question by Bleackk · Feb 15, 2016 at 02:49 PM · c#restart gamegamecontroller

Restarting a Level now that Application.LoadLevel is Obsolete

Since the only answer for my question is from Unity 4 it's not sufficient. What I need is a Current line for the code i have to make my game Restart when I press the R button. There is another problem my " Press R to restart " text is not showing up in the game after i blow myself up. Here's the Code, i have several Other Scripts too that this one Calls out to.

 using UnityEngine;
 using UnityEngine.SceneManagement;
 using System.Collections;
 
 public class GameController : MonoBehaviour {
 
     public GameObject hazard;
     public Vector3 spawnValues;
     public int hazardCount;
     public float spawnWait;
     public float startWait;
     public float waveWait;
 
     public GUIText scoreText;
     public GUIText restartText;
     public GUIText gameOverText;
 
     private bool gameOver;
     private bool restart;
     private int score;
 
     void Update ()
     {
         if (restart) 
         {
             if (Input.GetKeyDown (KeyCode.R)) 
             {
                 SceneManager.LoadScene(SceneManager.GetActiveScene().name);
             }
         }
     }
 
     void Start()
     {
         gameOver = false;
         restart = false;
         gameOverText.text = "";
         restartText.text = "";
         StartCoroutine (SpawnWaves ());
         score = 0;
         UpdateScore ();
     }
 
     IEnumerator SpawnWaves()
     {
         yield return new WaitForSeconds (startWait);
         while (true) 
         {
             for (int i = 0; i < hazardCount; i++) {
                 Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
                 Quaternion spawnRotation = Quaternion.identity;
                 Instantiate (hazard, spawnPosition, spawnRotation);
                 yield return new WaitForSeconds (spawnWait);
             }
             yield return new WaitForSeconds (waveWait);
 
             if (gameOver == true) 
             {
                 restartText.text = "Press 'R' to Restart";
                 restart = true;
                 break;
             }
         }
     }
 
     public void AddScore (int newScoreValue)
     {
         score += newScoreValue;
             UpdateScore();
     }
 
     void UpdateScore ()
     {
         scoreText.text = "Score: " + score;
     }
 
     public void GameOver ()
     {
         gameOverText.text = "Game Over ";
         gameOver = true;
     }
 }
 
Comment
Add comment · Show 1
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 SonicScrew12 · Feb 16, 2016 at 02:49 AM 0
Share

Are you using void OnGUI for your GUIText?

4 Replies

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

Answer by ByteVault · Feb 16, 2016 at 07:22 PM

This will load the current level you are currently running:

SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);

In the provided code you never run "GameOver()" function to set the gameover bool to true.

Comment
Add comment · Show 5 · 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 AndrewBilotti · Feb 16, 2016 at 09:35 PM 0
Share

thats exactly what I did, but I used Application.loadlevel ins$$anonymous$$d :)

avatar image tanoshimi AndrewBilotti · Feb 16, 2016 at 09:38 PM 0
Share

And that's why your code is going to break sooner than @xN0tiCx's... :)

avatar image AndrewBilotti tanoshimi · Feb 17, 2016 at 02:01 AM 0
Share

Well, Ive been using it for 5 years and so I remember all of the application.* stuff :P

avatar image ByteVault AndrewBilotti · Feb 17, 2016 at 12:46 AM 0
Share

The code that @Bleackk provided us is using the new "Scene$$anonymous$$anager" and therefore Im providing a snippet of a code which would be equal to what he seeks. What he is using is actually a working piece of code too and Im simply giving him another way of doing it.

I dont want to be rude @AndrewBilotti but the question is not really answered since he also states that the text "Press 'R' to Restart" is never shown. Which means that the "GameOver ()" function is never triggered to set "gameover" bool to "true". Which in return gives "restart" bool a constant state of false ending his ability to press the "R" key in order to change/restart the scene.

(Assu$$anonymous$$g all the other GUIText is working just fine since it is not stated otherwise.)

HOWEVER, I did enjoy the old "Application.loadlevel" very much and right now its not more than a warning and is working just fine. Later though, it will become an error.

Or am I completely off and should perhaps go take a well overdue nap? hehe

Cheers.

avatar image tubelightboy · Oct 31, 2020 at 05:19 PM 0
Share

Again, this does not work for games with lot of assets or the level which was loaded asynchronously.

avatar image
1

Answer by Geckoo · Sep 12, 2016 at 01:16 AM

You must use the new semantic, adding at first : using UnityEngine.SceneManagement;

Then, when you want restart a level, you can simply do : SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);

So, in order to load the next level, you can do : SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);

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 AndrewBilotti · Feb 16, 2016 at 02:52 AM

Simply, Application.LoadLevel(Application.LoadedLevelName);

This will simply reload the level.

Comment
Add comment · Show 8 · 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 EmHuynh · Feb 16, 2016 at 06:48 AM 2
Share

@AndrewBilotti Application.LoadLevel is obsolete. Use Scene$$anonymous$$anager LoadScene ins$$anonymous$$d.

avatar image AndrewBilotti · Feb 16, 2016 at 06:24 PM 1
Share

well, scene manager doesn't work as well, who gives a crap if application.loadlevel is obsolete? (I have like 245 warnings saying that every time I run any of my games) xD

avatar image AndrewBilotti · Feb 16, 2016 at 06:25 PM 0
Share

Also, what is with the down votes :(

avatar image meat5000 ♦ AndrewBilotti · Feb 16, 2016 at 06:42 PM 0
Share

Ive done Unity for 2 years and literally only discovered that App.LoadLevel is obsolete in the last 2 $$anonymous$$utes. This is a very recent thing.

I think the downvotes came from the "Who gives a crap" part in the comments :D

Its very important to address obsolescence.

avatar image AndrewBilotti meat5000 ♦ · Feb 16, 2016 at 09:33 PM 0
Share

xD Idk why it is obsolete but scene manager is buggy :o

avatar image tanoshimi AndrewBilotti · Feb 16, 2016 at 06:43 PM 0
Share

I imagine it's for giving bad advice... and you really should give a crap about those 245 warnings - they're there to help you, the developers didn't write them just for fun!

avatar image meat5000 ♦ tanoshimi · Feb 16, 2016 at 06:46 PM 1
Share

Indeed! Deprecation warnings now are errors later!

Show more comments
avatar image
0

Answer by aikijeet · Jan 11, 2017 at 04:14 AM

make sure you're using UnityEngine.SceneManagement;

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

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

11 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

Related Questions

how to write script to restart game after game over 1 Answer

Checking variables in GameController from a gameobject? 0 Answers

How to Restart game (Menu and Game are in same Scene)? 1 Answer

How to add a restart to my 2D game? 0 Answers

math.Lerp not correct? 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