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 Stimmerr · Mar 20, 2013 at 01:32 AM · saveload

Load/Save Game Issues, can't create new game.

I have a world manager script that controls pretty much everything, right now it all works and allows me to play through and checkpoint etc.. problem is: when I get to the last level and want to restart the game by death it loads my last checkpoint in level 3. im not sure how to re-write the variables for the new game state to make it start at level 1 again once they have been saved. I'm pretty new to StreamWriter so im stumped!

here is my code for the world manager

 ///////////////////////////////////////////////////////////////
 // SAVE / LOAD SYSTEM
 ///////////////////////////////////////////////////////////////
 static public function SaveGame( )
 {
     //  open the file for writing
     var file = new StreamWriter( Application.dataPath + "/Saves/SavedGame.txt");
     
     file.WriteLine( currentLevel );        // write the level the Player is on to the file
     file.WriteLine( CurrentSpawnPointIndex );        // write the Player SpawnPoint to the file
     
     // check if if the player exists so the information can be stored in the file
     if (GameObject.FindWithTag("Player"))
     {
         // grab the Stat file to grab the information
         var PlayerStats:PlayerStats = (GameObject.FindWithTag("Player").GetComponent("PlayerStats"));
                 file.WriteLine(PlayerStats.GetHealth());// write the health of the Player to the file
                 file.WriteLine(PlayerStats.GetAmmo(0));// write the Primary Ammo of the Player to the file
                 file.WriteLine(PlayerStats.GetAmmo(1));// write the Secondary Ammo of the Player to the file
     }
     
     // write the Mission status to the file
     file.WriteLine( Missions[0] ); //level1
     file.WriteLine( Missions[1] ); //level2
     file.WriteLine( Missions[2] ); //level3
 
 
     
     // make sure to write the information to the file
     file.Flush();
     
     // close the file
     file.Close();
     
 }
 
 public function SetStats( PlayerHealth : int ,AmmoPrime : int, AmmoAlt : int)
 { 
     var PlayerStats : PlayerStats = GameObject.FindWithTag("Player").GetComponent("PlayerStats");    
     PlayerStats.AddHealth(PlayerHealth, 0);
     PlayerStats.AddAmmo(0,AmmoPrime, 0);
     PlayerStats.AddAmmo(1,AmmoAlt, 0);
 }
 
 public function Initialize()
 {
     // set the state to Playing
     SetLevelState("Playing"); // used when working with coded buttons
     
     // load saved gameinfo if exists
     if( !Directory.Exists( "Assets/Saves/" ) )
     {    
         // if the directory doesnt exist, create it
         Directory.CreateDirectory( "Assets/Saves/" );
     }  
     
     // check to see if the file exists before tryign to read information
     if (File.Exists(Application.dataPath + "/Saves/SavedGame.txt"))
     {
         // file exists, open the file for reading
             var file = new StreamReader( Application.dataPath + "/Saves/SavedGame.txt");
 
         
         // read the information stored in the file and store them in variables
         currentLevel = parseInt(file.ReadLine());
         CurrentSpawnPointIndex = parseInt(file.ReadLine());
         PlayerHealth = parseInt(file.ReadLine());
         AmmoPrime = parseInt(file.ReadLine());
         AmmoAlt = parseInt(file.ReadLine());
         Missions[0] = parseInt(file.ReadLine());
         Missions[1] = parseInt(file.ReadLine());
         Missions[2] = parseInt(file.ReadLine());
         
         
         
         
         
         // close the file
         file.Close();
     }
     // file doesn exists, so create it with default values (New Game)
     else
     {
         // set default values for the New Game being created
         
         
         
         
         
         // open the file for Writing
         
     
         
         
         
         
         
         // write the information to the file
         
         
         
         
         
     }
     
     // load levels with the default or loaded values
     LoadingLevels();
     
     while(1)
     {
         var SpawnPlace : GameObject = GameObject.FindWithTag("spawnPoint" + CurrentSpawnPointIndex);        
         
         if(Application.GetStreamProgressForLevel(currentLevel) == 1 && SpawnPlace != null)
         {
             SpawnPlayer(CurrentSpawnPointIndex);
             SetStats(PlayerHealth, AmmoPrime, AmmoAlt);
             break;
         }
         else
         {
             yield WaitForSeconds(1);
         }
     }
     
 }
 
 //function used to load levels when game starts
 function LoadingLevels()
 {
     // check mission 1 status, if its not done, load level 1
     if(!Missions[0])
     {
         Application.LoadLevel(currentLevel);
     }
     
     // check mission 2 status, if its not done, load level 1 and 2
     else if(!Missions[1])
     {
         Application.LoadLevel(currentLevel);
         Application.LoadLevelAdditiveAsync(currentLevel+1);
             
                 
         Debug.Log("Loading Level 1");
     }
     // check mission 3 status, if its not done, load level 1, 2 and 3
     else if(!Missions[2])
     {
         Application.LoadLevel(currentLevel);
         Application.LoadLevelAdditive(currentLevel - 1);
         
         Debug.Log("Loading Level 2");
     }
     
 }
 
 //frontend
 function OnGUI()
 {
     if (Application.loadedLevel == 0 ) // start a New Game
     {
         // Create the Start button through code.
         if(GUI.Button(Rect(Screen.width - Screen.width/2, Screen.height - Screen.height/2, 180, 20), "New Game/ Continue"))
         {
             // start the game from where the player last did well
             Initialize();
         }
     }
     else if (levelState == "Dead") // is the Player dead
     {
         // Create the Load Last Checkpoint button through code
         if(GUI.Button(Rect(Screen.width - Screen.width/2, Screen.height - Screen.height/2, 180, 20), "Load last checkpoint"))
         {
             // start the game from where the player last did well
             Initialize();
         }
         
         // Create the Go to Main Menu button through code
         if(GUI.Button(Rect(Screen.width - Screen.width/2, Screen.height - Screen.height/2 + 30, 180, 20), "Go To Main Menu"))
         {
             // load the Start Screen
             Application.LoadLevel(0);
         }
     }
 }
 
 static public function MissionStatusCheck (missionIndex : int)
 {
     // what is the status of the mission
     return Missions[missionIndex];
 }
 
 // function is used to change the mission status, was it passed?
 static public function SetMissionStatus( missionIndex : int, status : int)
 {
     // status in the array is updated
     Missions[missionIndex] = status;
 }
 
 //changing level states
 static function SetLevelState(newState : String)
 {
     levelState = newState;
 }
 
 static public function SetCheckPoint( newCheckPoint : int )
 {
     CurrentSpawnPointIndex = newCheckPoint;
 }
 
 static public function SetCurrentLevel (newLevel : int)
 {
     currentLevel = newLevel;
 } 
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

0 Replies

· Add your reply
  • Sort: 

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

10 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

Related Questions

Problems with saving/loading score with PlayerPrefs [C#] 1 Answer

Save and Load Questions 0 Answers

Null Reference Exception even after check 1 Answer

Create a terrain prefab? Saving a terrain? No? 1 Answer

nice Save but wrong load 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