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
3
Question by Agoston Torok · Feb 10, 2010 at 07:54 AM · positionsaveloaddata

How can I save and load a player's position?

I'm designing a virtual navigation experiment, and i was wondering if i could somehow automatize my work. I need to present the same scene again and again, every presentation consists of adjusting player position first, than navigation, than if the subject says ready, save his position. So i thought about that maybe an algorithm could do it. The pure algorithm is something similar:

Load player position Let the player move the avatar Waiting for the subject to press ready Save position Do it again (approximately 20 times)

Could you help me out?

Agoston Torok, HAS, Institute of Psychology

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

5 Replies

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

Answer by duck · Feb 10, 2010 at 08:26 AM

Unity has a very simple built-in method of saving small amounts of data to the local machine using the PlayerPrefs class, which saves you from having to deal with explicitly reading and writing files. For example:

// to save an integer value PlayerPrefs.SetInt("Player Score", 10);

// to retrieve the integer value print (PlayerPrefs.GetInt("Player Score"));

And the commands are very similar for floats and strings:

PlayerPrefs.SetFloat("Player Lap Time", 29.3); print (PlayerPrefs.GetFloat("Player Lap Time"));

PlayerPrefs.SetString("Player Name", "Duck");
print ("Welcome back, " + PlayerPrefs.GetString("Player Name") + "!");

You could use this to write the x,y,z values of the player position and rotation.

You can also query the player prefs data to see whether a certain key has been set, using PlayerPrefs.HasKey, and delete keys using either PlayerPrefs.DeleteKey or PlayerPrefs.DeleteAll.

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 Eric5h5 · Feb 10, 2010 at 09:52 AM 0
Share

PlayerPrefs is only small amounts of data if you're using the web player, in which case it's 1$$anonymous$$B. Otherwise there are no limits. But yes, this is generally preferable to using System.IO since it's automatically cross-platform, and easier to deal with.

avatar image Sebas · Feb 10, 2010 at 06:54 PM 0
Share

I figured for psychology experiments you usually want everything in text files at the end to convert to Excel and lateron convert to statistical software for analysis. So I went for text files and System.IO. Not sure how the process with PlayerPrefs would be.

avatar image duck ♦♦ · Feb 10, 2010 at 09:30 PM 0
Share

Sure, if you want to create files in a format for other apps to read, using FileIO makes more sense. Using playerprefs only makes sense if the data is relatively small, and only needs to be read back by the unity file which wrote it.

avatar image ITInnovationTeam · Apr 20, 2015 at 03:30 PM 0
Share

How about arrays?

var pets = ["cats", "dogs", "fish"]; PlayerPrefs.SetArray(pets);

Would that work?

avatar image
3

Answer by Sebas · Feb 10, 2010 at 08:13 AM

Sounds very similar to my work (also psychology with focus on navigation). I load and save all my data from text files using streamwriter link.

Should look something like this.

function Update () {

if (Input.GetButtonDown ("Fire1")) { var fileName : String;

fileName = "C:\\test.txt";

var stream = System.IO.StreamWriter(fileName);

stream.WriteLine("test"); stream.Close();

} }

Load and save your positions in a textfile. So basically, load player position, set his transform accordingly, enable him walking through the environment and when he clicks or reaches his destination, just save and repeat the whole process. If you need more details, I am basically doing something very similar.

Drop me an email with what you do for your research (sebastian DOT koenig AT canterbury DOT ac DOT nz).

Comment
Add comment · Show 2 · 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 Agoston Torok · Feb 11, 2010 at 07:48 PM 0
Share

I've written you an email.

avatar image Neuropsyched · Sep 16, 2010 at 04:01 AM 0
Share

By the way, I utilized your script above in my experimental task and it works beautifully. Thank you!

avatar image
0

Answer by Agoston Torok · Feb 10, 2010 at 09:51 PM

Thanks to you I'm using the streamwriter, hope that this would work. And as you mentioned i prefer text files as output for further statistics, so the PlayerPrefs wouldn't be enough. I feel, that it needs some hours/days to finish it, but i will inform you about the progress os success. Thank you all!

Agoston Torok

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
avatar image
0

Answer by Agoston Torok · Feb 18, 2010 at 11:03 AM

Problem solved, it's working very well. Thank you very much!

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 KvanteTore · Mar 30, 2010 at 09:18 AM 0
Share

Please accept the correct answer :)

avatar image
0

Answer by Neuropsyched · Sep 16, 2010 at 03:17 AM

This is probably a dead thread but I just started in on using Unity for my experiments and wanted to talk to a few fellow psychologists about their experiences.

I just finished working up a reaction time experiment I'll be implementing on the iPad and am concerned about the accuracy of the times that are generated. I did a very VERY low tech check by comparing the Unity generated times with those of a stopwatch and the times seemed to check out. Specifically, the events in Unity and the stopwatch were started at about the same time and basically matched (assuming that the .04 second difference was due to human error in the synchronization). What are your experiences with RT? Feel free to contact me at dgs45 AT drexel DOT edu

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

No one has followed this question yet.

Related Questions

Not loading an object if it have be retained from a previous scene. 1 Answer

how do you save and make a pause menu? 3 Answers

Saving and Loading and assinging to gameobject 0 Answers

how to save a load a game? 1 Answer

What are the basics of saving and loading 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