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
1
Question by Nobozarb · Dec 09, 2017 at 09:18 PM · crashgametimescenesquit

Quit application before relaunching to specific scene

I am making a game where, if you take too long to complete the game, the universe begins to fall apart and the game "crashes" (the storyline is a little too complex and unnecessary to explain here). I will simulate the crash by setting time.timeScale to 0 to freeze everything, and then use close the application. I can use time.realtimeSinceStartup to measure time while everything is frozen. Anyway, after quitting, I want to be able to relaunch the application to a specific scene (in this case, an alternate universe). Is there a way to do this?

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
2
Best Answer

Answer by fdz_ · Dec 09, 2017 at 10:06 PM

Basically you are asking how to save player's progress. One way is with xml/json, the other (and the simplest way) is using PlayerPrefs

If you like videos, here's playlist by Board To Bits Games explaining different ways it's possible to save game data, plus, a little example of how to do what you want with PlayerPrefs:

     public bool fakeTheCrash;
     public string normalUniverseSceneName;
     public string alternativeUniverseSceneName;
     void Start()
     {
         int prefs_fakeCrash = PlayerPrefs.GetInt( "fakeCrash" );// using GetInt() because PlayerPrefs doesn't support Booleans
 
         if ( prefs_fakeCrash == 0 ) {// false
             UnityEngine.SceneManagement.SceneManager.LoadScene( normalUniverseSceneName );
         }
         else if ( prefs_fakeCrash == 1 ) {// true
             UnityEngine.SceneManagement.SceneManager.LoadScene( alternativeUniverseSceneName );
         }
     }
     private void Update()
     {
         if ( fakeTheCrash ) {
             fakeTheCrash = false;
             PlayerPrefs.SetInt( "fakeCrash", 1 );
         }    
     }


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 Nobozarb · Dec 09, 2017 at 11:54 PM 0
Share

Thanks for re$$anonymous$$ding me to save the player's data before "crashing". That would be helpful. But this doesn't answer my question about how to actually quit the application before relaunching it automatically.

avatar image fdz_ Nobozarb · Dec 10, 2017 at 03:28 AM 0
Share

So... yeah... I had some fun, and now I know how to do this on windows with the cmd.

Hopefully it may point you on the right direction if you need to do it on other platforms.


I made 3 scripts. Each one should be on a different scene.


Script in charge of loading the scenes:

 using UnityEngine;
 using UnityEngine.Scene$$anonymous$$anagement;
 public class SceneChooser : $$anonymous$$onoBehaviour
 {
     bool loadAlternativeWorld=false;
     private void Start()
     {
         loadAlternativeWorld = PlayerPrefs.GetInt( "loadAlternativeWorld" ) == 1;
         if ( loadAlternativeWorld )
             Scene$$anonymous$$anager.LoadScene( "Alternative World" );
         else
             Scene$$anonymous$$anager.LoadScene( "Normal World" );
         
     }
 }
 

The script for the Normal Scene (with the cmd stuff)

 using UnityEngine;
 using System.IO;
 using System.Diagnostics;
 public class NormalWorld : $$anonymous$$onoBehaviour
 {
     public string exeName="";
     void Update ()
     {
         if ( Input.Get$$anonymous$$eyDown( $$anonymous$$eyCode.Space ) ) {
             PlayerPrefs.SetInt( "loadAlternativeWorld", 1 );
         }
     }
     private void OnApplicationQuit()
     {
         if ( PlayerPrefs.GetInt( "loadAlternativeWorld" ) == 1 ) {
             var command1 = "TI$$anonymous$$EOUT /T 3 /NOBREA$$anonymous$$";
             var command2 = Path.GetDirectoryName( Application.dataPath ) + "/" + exeName + ".exe";
             //var command3 = "pause >nul";
             ExecuteCommand( command1 + " & " + command2 );
         }
     }
     void ExecuteCommand( string command )
     {
         Process process = new Process();
         // redirect the output stream of the child process.
         //process.StartInfo.UseShellExecute = false;
         //process.StartInfo.RedirectStandardOutput = true;
         //process.StartInfo.CreateNoWindow = true;
         process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
         process.StartInfo.FileName = "cmd.exe";
         process.StartInfo.Arguments = "/c" + command;
         process.Start();
         //var output = process.StandardOutput.ReadToEnd();
         //process.WaitForExit();
         //var exitCode = process.ExitCode;x
         //process.Dispose();
         //process = null;
     }
 }


And finally, the script for the Alternative Scene

 using UnityEngine;
 public class AlternativeWorld : $$anonymous$$onoBehaviour
 {
     void Update ()
     {
         if ( Input.Get$$anonymous$$eyDown( $$anonymous$$eyCode.Space ) ) {
             PlayerPrefs.SetInt( "loadAlternativeWorld", 0 );
         }
     }
 }


Sources from stackoverflow:

Execute multiple command lines with the same process using .NET

C# console app can't run a batch file but batch file works fine in command prompt

avatar image Nobozarb · Dec 12, 2017 at 06:32 PM 0
Share

I accepted this answer because it does answer my question, however I decided that it would work better to just freeze the game, make the screen pitch black, and then load a different scene, ins$$anonymous$$d of using the command line, which would be inefficient because I want my game to work on $$anonymous$$ac as well as PC.

avatar image fdz_ Nobozarb · Dec 12, 2017 at 07:31 PM 0
Share

That's good. That seams more reasonable than shutting and reopening the game.

Though... if you really wanted i think you could use a bash script.

How to use Process.Start() or equivalent with $$anonymous$$ono on a $$anonymous$$ac and pass in arguments

How make simple bash script mac

Equivalent of .bat in mac os

How to run shell script with C# on OSX?

I would like to run bash shell script through c#

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

105 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

Related Questions

Game Crashes On Startup? 3 Answers

Game App Crashes when Application.Quit() 0 Answers

Troubleshoot a Browser Based Game Crash 1 Answer

How to set the best time and save it? 1 Answer

Can you play a cutscene when entering an area? 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