• 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 · Jun 21, 2012 at 09:35 PM · positionprefabssaveloadstate

Help with Object position and state, save and load.

Hi, I have a number of "brick" prefabs in my scene (cubes with explosion script and scoretrigger scripts)

I have 4 prefabs, (all the bricks have been manually placed into my scene): bluebrick, greenbrick, redbrick & yellowbrick. Each are tagged with "brick".

What I want to be able to do is, when the player saves (from my pause menu), it stores each brick's position on screen and if it is destroyed or still there.

for example; if the player is playing and destroys 25/100 bricks and then saves the game, quits and reloads, the level will load and the 25/100 bricks will still be destroyed.

I have been searching for about a week and i'm just lost. Is there a way using PlayerPrefs to just save:

1) the position of the bricks. 2) whether it is there in scene or already been destroyed.

The latest thing I tried was using this script:

http://gamingirresponsibly.com/megabite-24-saving-and-loading

as you can see from my comment this didn't work out lol arrrgh!

please help

Comment
Add comment · Show 22
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 whydoidoit · Jun 21, 2012 at 09:46 PM 0
Share

Did you try out my Unity Serializer? I could help you trying to get that to work...

avatar image carter-carl30 · Jun 21, 2012 at 09:51 PM 0
Share

I had a look at it (i think it'd work) I watched your new video earlier. But I don't know how i'd fit it in with my save button on my pausemenu etc. I'm importing it now going to have a go at it :)

avatar image carter-carl30 · Jun 21, 2012 at 09:56 PM 0
Share

got this error when i imported it:

VerificationException: Error verifying TransformExtensions:LastAncestorOfType (UnityEngine.GameObject): Invalid generic method instantiation of method .Radical::FindImplementor (generic args don't respect target's constraints) at 0x001e UniqueIdentifier.FixName () (at Assets/Plugins/Serialization/UniqueIdentifier.cs:90) EditorNameFixer.Update () (at Assets/Plugins/Serialization/EditorNameFixer.cs:32)

avatar image whydoidoit · Jun 21, 2012 at 09:58 PM 0
Share

What from the asset store version???? Ugh - that's not supposed to have that file :(

avatar image whydoidoit · Jun 21, 2012 at 10:00 PM 0
Share

When I download it from asset store that EditorNameFixer isn't there...

avatar image whydoidoit · Jun 21, 2012 at 10:01 PM 0
Share

It should be very easy to integrate it. You just call the LevelSerializer.SaveGame("Your Game Name") function from your Save button.

avatar image carter-carl30 · Jun 21, 2012 at 10:04 PM 0
Share

i'm just downloading the latest one from your site (same as asset store) the one i had was older version! :) i'm just watching your video now.

avatar image whydoidoit · Jun 21, 2012 at 10:07 PM 0
Share

Take a look as TestSerialization.cs - that's a good example of how to do a simple GUI - it should do exactly what you want :)

avatar image carter-carl30 · Jun 21, 2012 at 10:08 PM 0
Share

ok will do :)

avatar image carter-carl30 · Jun 21, 2012 at 10:19 PM 0
Share

cool, I've got it working :)) brilliant!

how would i edit TestSerialization.cs to make the gui appear when i press "P"

this is my save script at the moment (below) obviously it'll now be defunt but i'd like it to appear on pause if possible. I don't want to try and edit your script or i'll end up messing it up lol :P

var paused : boolean = false; var pausedGUI : GUITexture; var mySound : AudioClip; var mySound1 : AudioClip;

pausedGUI.enabled = false;

function Update () { if(Input.Get$$anonymous$$eyUp("p")){ if(paused == true){
paused = false; } else { paused = true; }

 if(paused == true){
     audio.clip = mySound;
     audio.Play();
     Time.timeScale = 0.0;
     pausedGUI.enabled = true;
 } else {
     audio.clip = mySound1;
     audio.Play();
     Time.timeScale = 1.0;
     pausedGUI.enabled = false;
 }

} }

avatar image whydoidoit · Jun 21, 2012 at 10:24 PM 0
Share

I would just add a Save button to your existing script and then call LevelSerializer.SaveGame("BrickGame")

Something like:

     if(GUILayout.Button("Save"))
           LevelSerializer.SaveGame("BrickGame");

The put up a list of the saved games using buttons maybe a bit like this:

     GUILayout.BeginVertical();
     for(var sg in LevelSerializer.SavedGames[LevelSerializer.PlayerName])
     {
            if(GUILayout.Button(sg.Caption))
            {
                sg.Load();
            }
     }
     GUILayout.EndVertical();

If you are using GUI.Button you will need to have some rectangles that you change the Y coordinate on to put up the multiple save game buttons (or if you only want the last one you can just do:)

     if(LevelSerializer.SavedGames.Count > 0)
     {
        if(GUI.Button(Rect(100,100,100,50), "Load")
             LevelSerializer.SavedGames[LevelSerializer.PlayerName][0].Load();
     }
avatar image carter-carl30 · Jun 21, 2012 at 10:46 PM 0
Share

I don't know if i've done this correctly, i'm getting this error: Assets/Standard Assets/Scripts/pausescript.js(46,8): BCE0044: expecting ), found 'LevelSerializer'.

this is how my script is now:

var paused : boolean = false; var pausedGUI : GUITexture; var mySound : AudioClip; var mySound1 : AudioClip;

pausedGUI.enabled = false;

function Update () { if(Input.Get$$anonymous$$eyUp("p")){ if(paused == true){
paused = false; } else { paused = true; }

 if(paused == true){
     audio.clip = mySound;
     audio.Play();
     Time.timeScale = 0.0;
     pausedGUI.enabled = true;
 } else {
     audio.clip = mySound1;
     audio.Play();
     Time.timeScale = 1.0;
     pausedGUI.enabled = false;
 }
 

//testing new save script if(GUILayout.Button("Save")) LevelSerializer.SaveGame("BrickGame");

GUILayout.BeginVertical(); for(var sg in LevelSerializer.SavedGames[LevelSerializer.PlayerName]) { if(GUILayout.Button(sg.Caption)) { sg.Load(); } } GUILayout.EndVertical();

if(LevelSerializer.SavedGames.Count > 0) { if(GUI.Button(Rect(100,100,100,50), "Load") LevelSerializer.SavedGames[LevelSerializer.PlayerName][0].Load(); } //testing new save script

}

avatar image whydoidoit · Jun 21, 2012 at 10:49 PM 0
Share

Your problem is that your script is in Standard Assets - you can't call a C# script from a JavaScript script in Standard Assets. As your pause isn't really a standard asset you should just really move it somewhere else :)

avatar image whydoidoit · Jun 21, 2012 at 10:51 PM 0
Share

You only need one of those methods of showing a saved game too.

avatar image carter-carl30 · Jun 21, 2012 at 10:55 PM 0
Share

I moved it to it's own folder, still get this error:

Assets/pausescript/pausescript.js(46,8): BCE0044: expecting ), found 'LevelSerializer'.

avatar image carter-carl30 · Jun 21, 2012 at 11:10 PM 0
Share

Assets/pausescript/pausescript.js(30,11): BCE0044: expecting (, found 'OnGUI'.

i hate that red text in the status bar sometimes lol

avatar image whydoidoit · Jun 21, 2012 at 11:22 PM 0
Share

This is compiled and tested - but you would need to put your audio files back in - I'm using this as a JavaScript example in the next version of UnitySerializer :)

 var paused : boolean = false; 
 var pausedGUI : GUITexture;  
 
 pausedGUI.enabled = false;
 
 function Update () 
 { 
     if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.P))
     { 
        paused = !paused;
 
         if(paused == true){
             Time.timeScale = 0.0;
             pausedGUI.enabled = true;
         } else {
             Time.timeScale = 1.0;
             pausedGUI.enabled = false;
         }
    }
 }
 
 function OnGUI() {
     if(!paused)
        return;
        
     var box : GUIStyle = "box";   
     GUILayout.BeginArea(Rect( Screen.width/2 - 200,Screen.height/2 - 300, 400, 600), box);
 
     GUILayout.BeginVertical(); 
     GUILayout.FlexibleSpace();
     if(GUILayout.Button("Save Game"))
     {
        LevelSerializer.SaveGame("BrickGame");
     }
     GUILayout.Space(60);
     for(var sg in LevelSerializer.SavedGames[LevelSerializer.PlayerName]) { 
        if(GUILayout.Button(sg.Caption)) { 
          sg.Load(); 
          Time.timeScale = 1;
          } 
     } 
     GUILayout.FlexibleSpace();
     GUILayout.EndVertical();
     GUILayout.EndArea();
 
 
 }
avatar image carter-carl30 · Jun 21, 2012 at 11:28 PM 0
Share

cool :) i'm getting this error:

Assets/pausescript/pausescript.js(33,32): BCE0017: The best overload for the method 'LevelSerializer.SaveGame(String, boolean, function(String, boolean): void)' is not compatible with the argument list '(String)'.

I only have unity 3.5.2f2 (free version) just in case this has any bearing on things!

avatar image whydoidoit · Jun 21, 2012 at 11:35 PM 1
Share

Sorry - I also added a damn mod to the code on that Asset Store package :( Right so just make it:

  LevelSerializer.SaveGame("BrickGame", false, null);
avatar image carter-carl30 · Jun 21, 2012 at 11:53 PM 0
Share

ok, I put that in and... it works perfectly! :))

thankyou again for all your help and patience, really appriciate it

I used it to save my bricks, score and lives.

avatar image carter-carl30 · Jun 21, 2012 at 11:54 PM 0
Share

ps how do I put that latest code as the correct answer (as it's a comment)?

avatar image whydoidoit · Jun 22, 2012 at 12:00 AM 0
Share

I've added it as an answer - tick that :)

1 Reply

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

Answer by whydoidoit · Jun 21, 2012 at 11:57 PM

Solved by using Unity Serializer to store the position and existence of the bricks and added script to the pause menu to save and load the game.

 var paused : boolean = false; 
 var pausedGUI : GUITexture;  
 
 pausedGUI.enabled = false;
 
 function Update () 
 { 
     if(Input.GetKeyUp(KeyCode.P))
     { 
        paused = !paused;
 
         if(paused == true){
             Time.timeScale = 0.0;
             pausedGUI.enabled = true;
         } else {
             Time.timeScale = 1.0;
             pausedGUI.enabled = false;
         }
    }
 }
 
 function OnGUI() {
     if(!paused)
        return;
 
     var box : GUIStyle = "box";   
     GUILayout.BeginArea(Rect( Screen.width/2 - 200,Screen.height/2 - 300, 400, 600), box);
 
     GUILayout.BeginVertical(); 
     GUILayout.FlexibleSpace();
     if(GUILayout.Button("Save Game"))
     {
        LevelSerializer.SaveGame("BrickGame", false, null);
     }
     GUILayout.Space(60);
     for(var sg in LevelSerializer.SavedGames[LevelSerializer.PlayerName]) { 
        if(GUILayout.Button(sg.Caption)) { 
          sg.Load(); 
          Time.timeScale = 1;
          } 
     } 
     GUILayout.FlexibleSpace();
     GUILayout.EndVertical();
     GUILayout.EndArea();
 
 
 }
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Character Positions Not Serializing or Loading Properly? 1 Answer

saving the game in position 1 Answer

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

how to save last position for the player before he die 2 Answers

How can playerprefs save my tower model and its position? 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