Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 guilhermealles · Sep 19, 2014 at 01:03 PM · loadlevelresetrestart

How can I restart a single-scene game?

I built 2D board game on unity and I want to make it possible for the player to restart the game at any time (all I need to do is reset all the variables to their initial values and bring the stones to the original position). I don't know why, but Application.LoadLevel() with the same scene as before (I have only this scene) won't work for me. It does bring the stones to the original position, but I get a NullReferenceException whenever I try to play the game after restarting it.

The script I am using is as follows:

 using UnityEngine;
 using System.Collections;
 
 public class ResetGame : MonoBehaviour
 {
     void OnEnable()
     {
         Application.LoadLevel ("Madelinette");
     }
 }

Any idea about what is causing the problem?

Thanks for any help.

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 Subhajit-Nath · Sep 19, 2014 at 01:08 PM 0
Share
  1. You need to add the level to the build settings (shift + ctrl + b).

  2. Application.LoadLevel (Application.loadedLevel);

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by CHPedersen · Sep 19, 2014 at 01:06 PM

You are doing it correctly now. Application.LoadLevel is the way to go, even if you only have the one scene. Your NullReferenceException isn't caused by the approach being wrong, it's caused by some variable state elsewhere which doesn't handle scene transitions well. To fix that, we need more information, preferably the script where the NullReferenceException actually occurs along with the line number that identifies the culprit.

For starters, however, look for things that survive level changes and have the potential to start your game after a call to Application.LoadLevel with a different state than they have when the program starts altogether. These are scripts where you have called DontDestroyOnLoad, and, most likely, static variables.

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 guilhermealles · Sep 19, 2014 at 01:34 PM 0
Share

You got it! Looks like I had referenced some scripts in a class and they were all static variables. I removed that and it all worked properly. Thanks!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Reset The Scene ? 2 Answers

Resetting a Level from within itself 1 Answer

Current Level Load Again After Level Completion 2 Answers

Restart Mecanim's Animator controller 3 Answers

"Reset Level", Application.LoadLevel 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