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 LordZephyr · Feb 17, 2012 at 03:53 AM · javascriptiossaveloadlevels

Simple save/load scripting for multi level iOS game.

I understand (and have read) the many posts for saving and loading PlayerPrefs for iOS games but I think my problem is a little different especially because I have multiple levels in my game and I write in JavaScript.

I have 5 levels in my game and I'm trying to have a save game button during the course of the game and a load game button on the main menu. This is what I have so far: I created a small GUI Texture image of a small floppy disk in the upper right corner of all the level's screens and I use the OnMouseDown command to activate it but I don't know how to save the level and the position of the first person character when that icon button is pressed so that when the game is completely exited you can load it back to that spot when you select the load button on the main menu screen (of which I have the button working but I don't know how to script the load to get back to the last saved position).

I don't need to worry about anything else but the level and X,Y, and Z position of the player for that level.

Any help to write or suggestions as to where I can find the info to write the JavaScript I'm trying to write would be GREATLY appreciated.

Thanks so much in advance!!!

Tom

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 asafsitner · Feb 17, 2012 at 09:13 AM 0
Share

PlayerPrefs?

avatar image LordZephyr · Feb 17, 2012 at 04:00 PM 0
Share

@asafsitner, I read the link that you posted but it doesn't give me enough info to write my script. I'm sure that the script would be really simple, it's just that I can't seem to get all the parts together that I need. For example, I'm sure that I need to make a variable for the level but which would be better; a string for the level name or a float for the level number? Then, is the load just the reverse of the save? Thanks for the suggestion.

1 Reply

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

Answer by Julien-Lynge · Feb 17, 2012 at 04:51 PM

You didn't specifically ask any questions, so I'm going to address some of the things you mentioned. playerTransform is the transform of the player, which you'll have to figure out how to get (check out the GameObject.Find* methods).

  • Save the level: PlayerPrefs.SetInt("SavedLevel", Application.loadedLevel);

  • Save the player position: PlayerPrefs.SetFloat("PlayerX", playerTransform.position.x);

  • Same for y and z

Then, to load your saved data:

 if (PlayerPrefs.HasKey("SavedLevel"))
   Application.LoadLevel(PlayerPrefs.GetInt("SavedLevel"));

etc.

Comment
Add comment · Show 7 · 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 LordZephyr · Feb 17, 2012 at 08:12 PM 0
Share

I understand that I just stated that I had trouble and didn't ask a question. Sorry. Basically, my question is, How do I write a save and a load script. You gave me some ideas in your answer but they don't seem to be working for me. Here is my script for when the player touches the save icon on the screen:

function On$$anonymous$$ouseUp () { // This is for saving the level that the first person character is on. PlayerPrefs.SetInt("SavedLevel", Application.loadedLevel); // These three are the location as to where the first person character is on the above level. PlayerPrefs.SetFloat("PlayerX", playerTransform.position.x); PlayerPrefs.SetFloat("PlayerY", playerTransform.position.y); PlayerPrefs.SetFloat("PlayerZ", playerTransform.position.z); // This is just a dialog to be sure that I touched the save button. Debug.Log ("I touched the save icon"); }

And here is the script that I am trying to use for the Load button on the main menu when the player starts the game:

var touchRange: float = 100.0f;

function Update () { for (var touch : Touch in Input.touches) { if (touch.phase == TouchPhase.Began) { //Here you have the touch's position so, This is where you have to create a ray using ScreenPointToRay var userRay = Camera.main.ScreenPointToRay(touch.position); //This is to store the RayCasting results var info : RaycastHit; //[Physics.RayCast][2] actually casts the ray and if it returns true it "Hit" something if(Physics.Raycast(userRay, info, touchRange)) { //This tells us if the thing it hit was a "Door". if(info.transform.CompareTag("InstructionsTag")) { // This is where I am putting the change level info if (PlayerPrefs.Has$$anonymous$$ey("SavedLevel")) Application.LoadLevel(PlayerPrefs.GetInt("SavedLevel")); else //I'm using the following just in case the player taps the load button and nothing had been saved. Application.LoadLevel ("Level1"); Debug.Log ("I touched the Instructions Button!"); } } } } }

Could you please tell me what I am doing wrong and how I may fix it so that I may have a simple save and load for my iOS game.?

Thanks so much!!! I really appreciate any help you may offer.

$$anonymous$$

avatar image LordZephyr · Feb 17, 2012 at 08:16 PM 0
Share

I understand that I just stated that I had trouble and didn't ask a question. Sorry. Basically, my question is, How do I write a save and a load script. You gave me some ideas in your answer but they don't seem to be working for me. Here is my script for when the player touches the save icon on the screen:

function On$$anonymous$$ouseUp () {

// This is for saving the level that the first person character is on.

PlayerPrefs.SetInt("SavedLevel", Application.loadedLevel);

// These three are the location as to where the first person character is on the above level.

PlayerPrefs.SetFloat("PlayerX", playerTransform.position.x);

PlayerPrefs.SetFloat("PlayerY", playerTransform.position.y);

PlayerPrefs.SetFloat("PlayerZ", playerTransform.position.z);

// This is just a dialog to be sure that I touched the save button.

Debug.Log ("I touched the save icon");

}

avatar image LordZephyr · Feb 17, 2012 at 08:18 PM 0
Share

And here is the script that I am trying to use for the Load button on the main menu when the player starts the game:

var touchRange: float = 100.0f;

function Update () {

for (var touch : Touch in Input.touches) {

     if (touch.phase == TouchPhase.Began) {

     //Here you have the touch's position so, This is where 

you have to create a ray using ScreenPointToRay

     var userRay = Camera.main.ScreenPointToRay(touch.position);

     //This is to store the RayCasting results

     var info : RaycastHit;

     //[Physics.RayCast][2] actually casts the ray and if it returns true it "Hit" something

     if(Physics.Raycast(userRay, info, touchRange))

     {

     //This tells us if the thing it hit was a "Door".

     if(info.transform.CompareTag("InstructionsTag"))

     {

         // This is where I am putting the change level info

         if (PlayerPrefs.Has$$anonymous$$ey("SavedLevel"))

     Application.LoadLevel(PlayerPrefs.GetInt("SavedLevel"));

 else
     //I'm using the following just in case the player taps the load button and nothing had been saved.
     Application.LoadLevel ("Level1");
     Debug.Log ("I touched the Instructions Button!");
             }
         }
     }
 }

}

Could you please tell me what I am doing wrong and how I may fix it so that I may have a simple save and load for my iOS game.?

Thanks so much!!! I really appreciate any help you may offer.

$$anonymous$$

avatar image Julien-Lynge · Feb 18, 2012 at 03:33 AM 1
Share

$$anonymous$$,

I don't have time at the moment to look at your code, but a quick note:

playerTransform is something you need to define - it tells the code where to look to find the player position, rotation, etc. Perhaps define it in your Start function, something like this:

var playerTransform : Transform;

 function Start()
 {
   //here is where you define the transform for the player
   //look at the various find functions here (http://unity3d.com/support/documentation/ScriptReference/GameObject.html)
   //and choose the one that works best for your case

   //this should work if your player GameObject / Transform is name 'Player'
   playerTransform = GameObject.Find("Player");
 }

Apologies if that doesn't work out of the box - I'm a C# programmer and don't often work in JS.

avatar image Julien-Lynge · Feb 18, 2012 at 03:36 AM 1
Share

alternatively, if you put the script within the actual player (attach it as a component), then you can just use 'transform' - that always means 'the transform I'm attached to', so for instance

 PlayerPrefs.SetFloat("PlayerX", transform.position.x);

Here's a bit of reading on transforms: http://unity3d.com/support/documentation/Components/class-Transform.html http://unity3d.com/support/documentation/ScriptReference/Transform.html

Show more comments

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trying to save/load game, codes doesnt do anything. not even an error. 1 Answer

Saving lots of objects (Only answer in JavaScript) 1 Answer

Lowest common denominator of saving game information 0 Answers

the XML file doesn't work when i install the app from the app store 0 Answers

simple checkpoint/save/load system in javascript 4 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