Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by Hein06 · Oct 22, 2019 at 11:38 PM · savesave scenesave game

How do you save an instance of a game object in Unity C# after exiting the game?

I'm trying to save my checkpoint controller which logs the last checkpoint position that the player collided with and in another script loads the player by the last touched checkpoint on death. The only issue that I have is when I exit the build altogether. I have a PlayerPrefs.GetInt setup that saves the last scene the player was in but not the last checkpoint position they were in. Any suggestions?

 private static int currentScene
 public void LoadMenu()
     {
         currentScene = SceneManager.GetActiveScene().buildIndex;
         PlayerPrefs.SetInt("SavedScene", currentScene);
         SceneManager.LoadScene(7);
 
     }

Ignoring the main menu scene load. Is there any way to save the checkpoint controller's last logged position using the SetInt/String method?

Here's the checkpoint controller script:

 private static CheckpointController instance;
 public Vector3 previousCheckPointPos;
  
 
     // Start is called before the first frame update
     void Awake()
     {
         if (instance == null)
         {
             instance = this;
             DontDestroyOnLoad(instance);
             
             
         }
         else
         {
             Destroy(gameObject);
         }
     }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by rob11 · Oct 23, 2019 at 01:26 AM

Are the checkpoints always at same places ? If yes, I would save an Id through playprefs as an int. And when you want to load your character, you place it at the corresponding checkpoints position.

If these are moving or randomly placed checkpoints, I would divide the Vector3 position of the last checkpoint (or Vector2 if you're in 2D) into int variables. This time when you load, you recreate the vector with these variables to place your character at the right place.

For example :

 public void SaveCheckpoint()
     {
         PlayerPrefs.SetInt("LastCheckPointXpos", currentCheckpoint.transform.position.x);
         PlayerPrefs.SetInt("LastCheckPointYpos", currentCheckpoint.transform.position.y);
         PlayerPrefs.SetInt("LastCheckPointZpos", currentCheckpoint.transform.position.z);
 
     }
     public void LoadPlayer()
     {
         player.transform.position = new Vector3(PlayerPrefs.GetInt("LastCheckPointXpos"), PlayerPrefs.GetInt("LastCheckPointYpos"), PlayerPrefs.GetInt("LastCheckPointZpos"));
     }

But if you want to retrieve a specific Checkpoint for a list of checkpoints that are always the same, just save the index of the array and retrieve it at runtime. If they are randomly generated and you want to rebuild the entire object at runtime, you'll have to save all its variables and rebuild it through code. If you want to know more on the subject, you could read about data serialization, there's more efficient ways to save objects than always using playerprefs. Playerprefs are really handy when you need a couple variables but can get a little bit troublesome if you have too much.

Good luck!

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

117 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 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

Save Game Sceenes user quit aplication, Can I save the scene as a whole while leaving the game? 0 Answers

How to save High Scores 1 Answer

Saving Whole Scene ? 0 Answers

Objects instantiated in custom editor not saving. 0 Answers

How to Save and Load a Level by Serialization 3 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