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
0
Question by alirezamoosavi · Sep 18, 2018 at 05:37 PM · save datasave-to-file

Is there a way to save the player data (i.e. position) every 1 second automatically while he is playing?

Basically, I am creating a simple game for my project. I should store each player's data (i.e., position or velocity) at every time steps that I need (e.g., every 0.02 seconds) in a .txt file as an example and save it. I should do it since I need to use these data for post processing using other mathematical softwares like MATLAB. However, I do not have any experience with Unity. I followed a simple tutorial and designed my game, but I do not know how to save the data in the way that I want. I googled a lot and the only information that I find is about saving data like score and ... from one level to another level or saving data "Manually" by clicking a save button. Please note that I need to store the position of subject at every 0.02 seconds and then save it. Is there anyone that could help me with this or at least tells me how can I do it? Thanks

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 vkishan96 · Jul 03, 2019 at 08:29 AM 0
Share

please do share the code if you have achieved this. Even iam trying to do the same

avatar image xxmariofer vkishan96 · Jul 03, 2019 at 09:06 AM 0
Share

you cant save to an application, you save to a file, and you open that file with the application, not tested but should work

 void Awake()
 {
         string path = Application.dataPath + "/Test.txt";
 
         //Write some text to the test.txt file
         StreamWriter writer = new StreamWriter(path, true);
 }
 
 void Update()
 {
         writer.WriteLine(transform.position,x + ", " + transform.position.y + ", " + transform.position.z);
 }
 
 void OnDestroy() { writer.Close() }

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Casiell · Sep 18, 2018 at 06:41 PM

From what you wrote I assume you know how to save your data, as in write a function that will gather all the information you need and save it to a file.

What you need to do is create a script that derives from MonoBehaviour. You now have access to Update which is automatically called every single frame.

Now you want to create a float variable (outside of Update) that will be used as a 'timer'.

In Update add Time.DeltaTime to the timer until it's bigger than your required time step. When it is, just reset the timer and save your game. Example:

 public class Saver : MonoBehaviour
 {
     private const float TimeStep = 0.02f; //this is in seconds
     private float _timer = 0;
     
     private void Update()
     {
         timer += Time.DeltaTime;
         if(timer >= TimeStep)
         {
             timer = 0;
             Save();
         }
     }
 }

What you may want to consider is storing your position or other data in an array or other structure and only save to file once in a while to reduce performance hit from saving. Of course this may not be necessary depending on the rest of your program or you may just not want to do that, it's just a suggestion.

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

90 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

Related Questions

How to save an instance of a class(Make a custom component?) from the editor window 0 Answers

Racing Game: Saving and loading replay data? 1 Answer

How to save custom binary files to iCloud? 0 Answers

Saves on android, could not find file 0 Answers

how to have save and load system. With this programing,How to do a save and load system on mobile. With this programing 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