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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by carter-carl30 · May 23, 2012 at 11:06 PM · saveload

Help with my save and load script please

I am trying to figure out how to do a simple save and load script so the player can press save (during any level via a 3d text with colision on each level) and then if they die or quit they can reload the level they reached by pressing load from the menu screen.

I saw this question http://answers.unity3d.com/questions/218114/simple-saveload-scripting-for-multi-level-ios-game.html and used the info from answer number 1 to see if I could make my own (see below).

MY SAVE SCRIPT:

 #pragma strict
 //put this script on save button
 var mySound : AudioClip;
 var mySound1: AudioClip;
 
 
 function OnMouseEnter()
 {
     //change color of the text
     renderer.material.color = Color.red;
     audio.clip = mySound;
     audio.Play();
 }
 
 function OnMouseExit()
 {
     //change the color of the text
     renderer.material.color = Color.white;
 }
 
 function OnMouseUp() { 
 
               //Save level
               audio.clip = mySound1; 
               audio.Play();
               PlayerPrefs.SetInt("SavedLevel", Application.loadedLevel); 
 }

=========

I also have made this load/continue script (put on a 3d text with collider), (this is on my main menu screen) below:

MY LOAD/CONTINUE SCRIPT

 #pragma strict
 //put this script on load/continue button
 var mySound : AudioClip;
 var mySound1: AudioClip;
 
 
 function OnMouseEnter()
 {
     //change color of the text
     renderer.material.color = Color.red;
     audio.clip = mySound;
     audio.Play();
 }
 
 function OnMouseExit()
 {
     //change the color of the text
     renderer.material.color = Color.white;
 }
 
 function OnMouseUp() { 
 
               audio.clip = mySound1; 
               audio.Play();
               
               //Load level
               if (PlayerPrefs.HasKey("SavedLevel"))
                 Application.LoadLevel(PlayerPrefs.GetInt("SavedLevel"));
 }

========================

To test this out, I put a save button on level 2 of my game and played it for a few seconds then pressed my save button. I then purposefully lost all my lives and from my game over screen went back to my main menu and pressed my load button.

When I pressed it it loaded level 2 (cool!) but it quickly went to my game over screen as if it had saved at the point I lost all my lives and NOT at the point that I pressed save (after a few seconds of play on level 2).

Is there something I have missed out or doing wrong? please advise me if you can :)

PS this is in javascript and it's for Webplayer/PC


UPDATED 29/05/2012


@Mike (whydoidoit)

NOW ON MY SAVE SCRIPT:

function OnMouseUp() {

           //Save level
           audio.clip = mySound1; 
           audio.Play();
           PlayerPrefs.SetInt("SavedLevel", Application.loadedLevel);
           PlayerPrefs.SetInt("Score", Scorecounter);
           PlayerPrefs.SetInt("Lives", LIVES);

}

=====================

NOW ON MY LOAD SCRIPT

function OnMouseUp() {

           audio.clip = mySound1; 
           audio.Play();
           
           //Load Saved score, lives and then load saved level
           PlayerPrefs.GetInt("SavedLevel", Application.loadedLevel);
           PlayerPrefs.GetInt("Score", Scorecounter);
           PlayerPrefs.GetInt("Lives", LIVES);
             Application.LoadLevel(PlayerPrefs.GetInt("SavedLevel"));

}

===================

THIS IS MY SCORE TRIGGER SCRIPT

function OnTriggerEnter(enterer : Collider) { if (enterer.collider.gameObject.CompareTag("Bullet")) { Debug.Log("Score!"); Scorecounter.Counter ++; } }

=======================

THIS IS MY SCORE COUNTER SCRIPT

static var Counter : int = 0;

function Update () {
guiText.text = "Score: " + Counter; }

MY LIVES SCRIPT

var lives : Texture2D; //one life left var lives2 : Texture2D; //one life left var lives3 : Texture2D; //one life left var lives4 : Texture2D; //one life left var lives5 : Texture2D; //one life left var lives6 : Texture2D; //one life left var lives7 : Texture2D; //one life left var lives8 : Texture2D; //one life left var lives9 : Texture2D; //one life left

static var LIVES = 5;

function Update () { switch(LIVES) {

     case 9:
         guiTexture.texture = lives9;
     break;
         
     case 8:
         guiTexture.texture = lives8;
     break;
             
     case 7:
         guiTexture.texture = lives7;
     break;
         
     case 6:
         guiTexture.texture = lives6;
     break;
         
     case 5:
         guiTexture.texture = lives5;
     break;
         
     case 4:
         guiTexture.texture = lives4;
     break;
         
     case 3:
         guiTexture.texture = lives3;
     break;
     
     case 2:
         guiTexture.texture = lives2;
     break;
     
     case 1:
         guiTexture.texture = lives;
     break;
     
     case 0:
         Application.LoadLevel(54);
 }    

}

THE PART IN MY FLOOR COLISION SCRIPT THAT REMOVES A LIFE WHEN BALL HITS THE FLOOR

lives.LIVES -= 1;

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 whydoidoit · May 29, 2012 at 05:00 PM

So you have a number of choices:

Choice one:

  • You save every variable you want using PlayerPrefs.SetInt("NameOfSavedVariable", savedVariable); or string or whatever

  • You read your values back in Start() using savedVariable = PlayerPrefs.GetInt("NameOfSavedVariable")

Choice two:

  • You use XmlSerializer. This will serialize a class into an XML string that you can save in PlayerPrefs using PlayerPrefs.SetString and then you can deserialize it when you start up.

  • You probably want to make a helper class to hold all of your game state as serializing a MonoBehaviour isn't going to do it for you.

  • Mark the class with a [Serializable] attribute.

Choice three:

  • Use something like EZ Game Saver from the asset store that will help you out with a bunch of things

Comment
Add comment · Show 8 · 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 carter-carl30 · May 29, 2012 at 07:09 PM 0
Share

So if saved the score, level reached and lives left like this:

PlayerPrefs.SetInt("SavedLevel", Application.loadedLevel); PlayerPrefs.Setint("Score"); PlayerPrefs.Setint("Lives");

would that work? or do I need to set an variable for say wall 1, wall 2, roof, floor, pad, for everything in the scene?

would I be correct in thinking in the ("") I would have to put the name of my score counting script and lives counting script?

sorry for all the questions! it's very hard to being new to javascript and scripting

avatar image whydoidoit · May 29, 2012 at 07:14 PM 0
Share

You're ok, don't worry - we were all noobs once.

Yes I think you are fine saving just those things, just so long as you don't expect it to load with half a level complete or something. $$anonymous$$g. where things have moved from the starting point of that level. If things are different to how it would be at load level and you want to get back to exactly that state then you need to save information so you know what to change to make it the same as when it was saved.

It's:

   PlayerPrefs.SetInt("Score", whateverYourScoreVariableIsCalled);

to save your score and

  whateverYourScoreVariableIsCalled = PlayerPrefs.GetInt("Score");

etcetera

avatar image carter-carl30 · May 29, 2012 at 07:45 PM 0
Share

ok so for example to save my score, I have a GUItext called "scoretext" with a script called "Scorecounter".

So would I do this like:

PlayerPrefs.SetInt("Score"), GameObject.scoretext; ??

avatar image whydoidoit · May 29, 2012 at 08:24 PM 0
Share

Your score must be in a variable right? Not just the Guitext or how would you add on to it?

It's

PlayerPrefs.SetInt("Score", score);

You won't get it off GameObject directly and your bracket is in the wrong place. $$anonymous$$aybe post some more code by editing your questoin?

avatar image carter-carl30 · May 29, 2012 at 10:38 PM 0
Share

Hi $$anonymous$$ike, I have updated my question (scroll down a bit) to include what I now have and info on my lives and score.

Show more comments
avatar image
0

Answer by aldonaletto · May 24, 2012 at 12:15 AM

I'm sorry to inform you that the only thing saved in your code is the level index - not even a single bit of other level data was saved.
Saving and loading level information is a much more complex subject: you must first define what exactly must be saved - each item will require a PlayerPrefs.SetInt, SetFloat or SetString to be saved.
But saving and retrieving player data (position, health, score, lives, weapons etc.) is easy; handling other scene objects may be much more complicated: collectable items like weapons, ammunition, health packs etc. must be managed - when loading a saved level, already collected items must be deleted or not instantiated. Enemies are even harder: at least a dead/alive status for each enemy must be saved, so dead enemies will not respawn from hell when the saved level is loaded - and in more complex cases you may also have to save/retrieve enemy's position, rotation, health, AI status etc.

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 carter-carl30 · May 24, 2012 at 05:23 PM 0
Share

@Aldo, thanks for the response. Sounds alot more complicated than I thought! The game I am making is a breakout/araknoid type game, I think the only things I would need to save are the players current level reached, lives and current score.

Would I do this by

PlayerPrefs.Setint("Score"); PlayerPrefs.Setint("Lives");

etc... would I be correct in thinking in the ("") I would have to put the name of my score counting script and lives counting script?

Or do I have to set a value for everything that is in my scene?

sorry if I sound really dumb

avatar image carter-carl30 · May 29, 2012 at 04:50 PM 0
Share

anyone help me?

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

6 People are following this question.

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

Related Questions

Serialization scripting error 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

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

Load and Save Scenes with PlayerPrefs.Set/GetInt 0 Answers

Not loading an object if it have be retained from a previous scene. 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