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 finalfire1 · Mar 12, 2014 at 09:17 PM · loadsave scene

Creating a Save/Load game

Okay so what I want to do is this.

First I want to be able to save any changes that my player makes with any object in the scene. For example, say he picks up an object, then moves it across the room, and then drops it, leaves the scene, then returns later. I'd like the object to be in the last place he left it. The same with other things like destructibles, enemies, etc.

I figure that the best way to do this would be through a temporary file that is saved while the player is playing. I'm not sure how to create this though, and I'm also not sure how to make said file, and am having trouble finding/understanding the documentation. (Would PlayerPrefs be able to do what I'm asking?)

Then also I would like him to be able to save all the changes into a permanent save file which he can later load, and pick up where he left off.

Thank you for all your help!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by AFDozerman · Mar 24, 2014 at 12:36 AM

What language are you using? This is how to create a file in c#:

http://stackoverflow.com/questions/802541/creating-an-empty-file-in-c-sharp

It essentially boils down to

String path = Directory.GetCurrentDirectory();

String OP = "Sending this to file";

Using(System.IO.File.Create("textfile.txt"));

System.IO.File.WriteAllText(textfile.txt,OP);

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 finalfire1 · Mar 27, 2014 at 09:30 PM 0
Share

Thanks, this was very helpful. For those that are interested, this is the basic script I was able to come up with:

 //This is really cluttered, and not the best, but it works as an excellent
 //example for anyone that wants to build a basic save game feature
 #pragma implicit
 import System;
 import System.Runtime.Serialization.Formatters.Binary;
 import System.IO;
 //If set to true it will save the transform once exactly
 var save : boolean;
 //Get a binary formatter
 var serializer = BinaryFormatter();
 //Create an in memory stream
 var m = $$anonymous$$emoryStream();
 
 function Start () 
     {
     //Checking for our save game
     if(File.Exists(Application.persistentDataPath + "/highscores.dat"))
         {
         //Opening the file
         var f = File.Open(Application.persistentDataPath + "/highscores.dat", File$$anonymous$$ode.Open);
         //Restoring the class we serialized
         var loader = serializer.Deserialize(f);
         //setting the values.
         this.transform.position.x  = loader.transformX;
         this.transform.position.y  = loader.transformY;
         this.transform.position.z  = loader.transformZ;
         }
     }
 
 function Update () 
     {
     if(save == true)
         {
         //Creating our file to save to
         var f = File.Create(Application.persistentDataPath + "/highscores.dat");
         //The class we're going to use to store our data
         var saveThing = new SaveData();
         //Setting the values
         saveThing.transformX = this.transform.position.x;
         saveThing.transformY = this.transform.position.y;
         saveThing.transformZ = this.transform.position.z;
         //Serialization of our class
         serializer.Serialize(f,saveThing);
         //ending our save
         save = false;
         }
     }
 
 //A class to store our data
 class SaveData extends System.Object
     {
     var transformX;
     var transformY;
     var transformZ;
     }`
avatar image
0

Answer by xt-xylophone · Mar 24, 2014 at 01:26 AM

This tutorial explained everything for me. Highly recommend!

Comment
Add comment · 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

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

22 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

Related Questions

Loading and saving default files on Android 1 Answer

Saving your scene and location in game? 1 Answer

How to load player last scene visited 1 Answer

Scene Saving 1 Answer

script to save and load a game 3 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