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 drsv · Mar 20, 2014 at 07:52 AM · loadingsaving

Problem in loading and saving the game

Hello all,

While making save game script i took the following attributes

1)Current level name 2)player location and ammo count

while loading I loaded the saved level name and set the player location and ammo. And its working properly but the loaded game is not lasting even for a second and the level with the default settings and variables.

Here is the code:

 import System.IO;
 
 private var buttonWidth : int;
 private var buttonHeight : int;
 private var groupWidth : int;
 private var groupHeight : int;
 var paused : boolean;
 var character : GameObject;
 var shootObject : GameObject;
 var xpos : float;
 var ypos : float;
 var zpos : float;
 function Start () 
 {
     buttonWidth = 200;
     buttonHeight = 50;
     groupWidth = 200;
     groupHeight = 500;
     Screen.lockCursor = true;
     Time.timeScale = 1;
     paused = false;
     //shootObject = GameObject.FindGameObjectWithTag("Gun");
 }
 function OnGUI()
 {
     if(paused)
     {
         GUI.BeginGroup(new Rect(((Screen.width/2) - (groupWidth/2)), ((Screen.height/2) - (groupHeight/2)), groupWidth, groupHeight));
         if(GUI.Button(new Rect(0, 0, buttonWidth, buttonHeight),"Main Menu"))
         {
             Application.LoadLevel(0);
         }
         if(GUI.Button(new Rect(0, 60, buttonWidth, buttonHeight),"Save Game"))
         {
             var path : String = "C:/Aditya/TestSaves";
             if (!Directory.Exists(path))
             {
                 Directory.CreateDirectory(path);
             }
             if(!File.Exists(path + "/SaveSlot1.txt"))
             {
                 var create_text = File.CreateText(path + "/SaveSlot1.txt");
                 create_text.Close();
             }
             var loadedfile = new StreamWriter(path + "/SaveSlot1.txt");
             loadedfile.WriteLine(saveData());
             loadedfile.Close();
         }    
         if(GUI.Button(new Rect(0, 120, buttonWidth, buttonHeight),"Load Game"))
         {
             loadData();
         }
         if(GUI.Button(new Rect(0, 180, buttonWidth, buttonHeight),"Quit"))
         {
             Application.Quit();
         }
         GUI.EndGroup();
     }
 }
 function Update () 
 {
     if(Input.GetKeyUp(KeyCode.Escape))
     {
         if(Time.timeScale == 0)
         {
             GameObject.Find("First Person Controller").GetComponent(MouseLook).enabled = true;
             Screen.lockCursor = true;
             Time.timeScale = 1;
             paused = false;
         }
         else
         {
             GameObject.Find("First Person Controller").GetComponent(MouseLook).enabled = false;
             Screen.lockCursor = false;
             Time.timeScale = 0;
             paused = true;
         }
     }
 }
 
 function saveData()
 {
     var data : String;
     xpos = character.transform.position.x;
     ypos = character.transform.position.y;
     zpos = character.transform.position.z;
     data += "LevelName:" + Application.loadedLevelName + "\n";
     data += "X-Position:" + xpos.ToString() + "\n";
     data += "Y-Position:" + ypos.ToString() + "\n";
     data += "Z-Position:" + zpos.ToString() + "\n";
     data += "Ammo:" + shootObject.GetComponent(shoot).ammo + "\n";
     
     return data;
 }
 
 function loadData()
 {
     var path : String = "C:/Aditya/TestSaves";
     if(File.Exists(path + "/SaveSlot1.txt"))
     {
         var loadeddata = File.OpenText(path + "/SaveSlot1.txt");
         var readdata = loadeddata.ReadLine();
         while(readdata != null)
         {
             var getdata = readdata.ToString().Split(":" [0]);
             //if(getdata[0] == "LevelName") Application.LoadLevel(getdata[1]);
             if(getdata[0] == "X-Position") character.transform.position.x = float.Parse(getdata[1]);
             if(getdata[0] == "Y-Position") character.transform.position.y = float.Parse(getdata[1]);
             if(getdata[0] == "Z-Position") character.transform.position.z = float.Parse(getdata[1]);
             if(getdata[0] == "Ammo") shootObject.GetComponent(shoot).ammo = float.Parse(getdata[1]);
             readdata = loadeddata.ReadLine();
         }
         loadeddata.Close();
     }    
 }


Someone help me as soon as possible.

Thanks, Aditya

Comment
Add comment · Show 2
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 fafase · Mar 20, 2014 at 07:54 AM 0
Share

Do you get any error? Also, don't you want to use PlayerPrefs ins$$anonymous$$d? It does pretty much the same as you do but in one line.

Not to mention the fact that you save as plain text and can then be modified really easy.

avatar image whydoidoit · Mar 20, 2014 at 08:35 AM 0
Share

or check out this tutorial on just saving classes with your data in them:

http://unitygems.com/saving

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

21 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

Related Questions

How can I save a character during runtime using script? 3 Answers

Saving Game Confussion 1 Answer

Saving frequently throughout game lifecycle (Android) 1 Answer

System.IO Help! 2 Answers

Loading a saved Game 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