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 /
This question was closed Aug 18, 2016 at 09:34 PM by ethranes for the following reason:

After the help from the people below, some soul searching through google and ultimately this tutorial video - https://www.youtube.com/watch?v=uw0kZ72zCvE I was able to resolve the issue

avatar image
0
Question by ethranes · Aug 17, 2016 at 07:33 PM · c#playerprefssave datasetactivedontdestroyonload

PlayerPrefs to save setActive state

Hi,

I have some script that is run from my Game scene which hides a gameObject on my Menu scene. However each time the game is closed and reopened the gameObject in my Menu scene is set back to active. I want the gameObject that's been hidden to remain hidden, even through a game reload. This is the code that I have at the moment.

         DontDestroyOnLoad(gameObject);
         SceneManager.LoadScene("sceneSelectBeta");
         GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag ("Level2Block");
         foreach(GameObject go in gameObjectArray) {
             go.SetActive (false);
             PlayerPrefs.????? ("?????", ?????);
         }
         DestroyObject(gameObject);

I use PlayerPrefs to save other information in my project, but I'm not sure how to implement it in this case, any help or advice would be much appreciated.

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

  • Sort: 
avatar image
1

Answer by Mukabr · Aug 17, 2016 at 07:52 PM

Try using this so you can save arrays of booleans and than set/get.

Comment
Add comment · Show 3 · 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 ethranes · Aug 17, 2016 at 09:18 PM 0
Share

I'm aware of the different functions of the PlayerPrefs, but I'm not sure of a way to implement them in my current code.

avatar image Mukabr ethranes · Aug 17, 2016 at 09:28 PM 0
Share

$$anonymous$$eep in $$anonymous$$d that the link that I posted is not the same as the Unity's PlayerPrefs. About the aplication, you could do something like this to save the active state

  void Start () {
         DontDestroyOnLoad(gameObject);
         Scene$$anonymous$$anager.LoadScene("sceneSelectBeta");
         GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag("Level2Block");
         bool[] isActive = new bool[gameObjectArray.Length];
         foreach (GameObject go in gameObjectArray)
         {
             go.SetActive(false);
             isActive[i] = false;
         }
         PlayerPrefsX.SetBoolArray("Level2Block", isActive);
         DestroyObject(gameObject);
     }


And than something like this to get the active state

 GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag("Level2Block");
         foreach (GameObject go in gameObjectArray)
         {
             go.SetActive(PlayerPrefsX.GetBoolArray("Level2Block")[i]);
         }
avatar image ethranes Mukabr · Aug 18, 2016 at 03:20 PM 0
Share

Thanks for the suggestion, I haven't been able to get this working in my project at the moment, I'm adding the first block of code to the start function of my game scene and getting 2 errors 'The name 'i' does not exist in the current content' and 'The name 'PlayerPrefsX' does not exist in the current context.

I'm also unsure where the second block of code should be put, should it be added to the game scene also, or should it be put in the menu scene? Thanks for the help you've given.

avatar image
1

Answer by g__l · Aug 18, 2016 at 03:36 PM

In player prefs you can only save three types of variable int, float and string. In order to save a bool i would suggest saving it as an int where if that variable is 1 the state is true and 0 it is false.

(An Example)

To set an int use PlayerPrefs.SetInt("Active" , 0)

Then to get it use PlayerPrefs.GetInt("Active") which returns the value.

A link to the documentation - https://docs.unity3d.com/ScriptReference/PlayerPrefs.html

If you require any further assistance just ask, I hope that helps.

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 ethranes · Aug 18, 2016 at 07:29 PM 0
Share

Thanks @g_l for the help. This is the code at the moment, it is able to compile but it still is setting the object back to active after reloading the game.

     PlayerPrefs.SetInt ("Set Active", 1);

     DontDestroyOnLoad(gameObject);
     GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag ("Level2Block"); //find game object with tag
     foreach(GameObject go in gameObjectArray) {
         go.SetActive (false);
         bool active = PlayerPrefs.GetInt("Set Active" , 1) == 0;
     }

Follow this Question

Answers Answers and Comments

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

How to stop objects you picked up from reappearing when you go to another level and return 1 Answer

PlayerPrefs: loading saved data from the menu? 1 Answer

Save and Load Instantiate Enemies 2 Answers

Is this a good way of resetting stats? 1 Answer

Multiple Cars not working 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