Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
avatar image
0
Question by NayrKun · May 16, 2021 at 11:10 AM · restartrestart game

Restart button for multiple scenes?

I am new to programming, and I wanted to create a multiple level or scenes for my game. First I made a scene for a game over screen and a button for restarting. My problem is that I don't know the code or a way for this restart button to restart the game without going back to other scenes. Here's an example: I died on level 1, game over screen shows up, I then presses the restart button, it goes to level 1 instead.

If there is a way for it to go back please do tell. Thank you!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by In0sc0p3dJFK · May 16, 2021 at 11:30 AM

Just reload the scene.

Firstly, you're going to have to add a new directive. If you're not familiar with what that is, it's basically just the little lines of code at the veryyy top of your script such as:

 using UnityEngine;
 using System.Collections;
 
 //code

You're going to want to add the Unity.SceneManagement directive. so you can just add it under the other directives like this:

     using UnityEngine;
     using System.Collections;
     using Unity.SceneManagement;
 
 // code

If you were to give your code, this would be alot easier. But in whatever void or anything that triggers the Game Over scene, you have to just call the same scene again.

 using UnityEngine;
 Using System.Collections;
 Using Unity.SceneManagement;
 
      void GameOver()
 {
      SceneManager.LoadScene(SceneManager.GetActiveScene());
 }

I'm just assuming you have a void for the GameOver. Also, i'm assuming you want the game over screen on the screen for a couple of seconds before the scene restarts, so for that, you're going to want to use a Coroutine. But I wont get into that unless you need me to. If you have something like a scrolling background, the background might not reset with the scene so for that you might want to use Time.time and Time.deltaTime. But again, I won't get into that unless you really need it. I'm just assuming alot of stuff here. You might want to give us a some code as an example.

Comment
Add comment · Show 4 · 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 NayrKun · May 17, 2021 at 08:24 AM 0
Share

Hello, thank you for answering, but the GetActiveScene part won't go back to the first level because the active scene is the game over screen, and I'm sorry that I didn't revise my question but the one I'm asking is how to restart the scene based on what level the player is currently in.

 using UnityEngine;
     using UnityEngine.SceneManagement;
     
     
     public class RestartButton : MonoBehaviour
     {
         public int Index;
     
         public void ResetScene()
         {
             SceneManager.LoadScene(Index);
             
         }
     }
 

This is the script that I'm using.

avatar image In0sc0p3dJFK NayrKun · May 17, 2021 at 06:48 PM 0
Share

oh ok. So you have to actually set the scene index outside of the script in unity. At the top left of your Unity project, there should be a a button called "Files". This website, for some reason, doesn't let me attach files so work with me here. It should drop down to reveal a bunch of options. One of the last options, there should be one called "Build Settings". If you open that up, there should be another window that pops up. It says "Scenes in Build". Drag all of your scenes from your project tab to that space there, in order. And if you already have a Build Index set up, it should start working if you use that same line of code you just showed. THIS WILL ONLY WORK if you are only using the game scene, and the game over scene. Any other scene put into this build will not work. And you're going to to have to load the first scene again.

Also, I noticed that you are using a button to restart? You need to add a void named something like "OnClickResetScene". But I wont get into that if you don't need me to.

avatar image NayrKun In0sc0p3dJFK · May 19, 2021 at 09:00 AM 0
Share

I actually have done this, and it works perfectly, if I only have a single level. But my question is, is there a script or code for restarting the game based on what level the player is in. Is the script I'm using not applicable for having multiple levels/scenes on a single game?

Show more comments

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

118 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

Related Questions

Simple Restart not working 0 Answers

Creating a Restart Button 3 Answers

scene won't reset after restart 1 Answer

My game keeps restarting at random 1 Answer

Full scene restart 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