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 artem_levitin · Jan 03 at 08:01 AM · scripting problem

How to save GameObject activity via Playerprefs

I want to save the active and inactive states of each individual game object using PlayerPrefs, because when rebooting, all state data is reset. I do not know what to do, help me!

 public GameObject GiveButton1;
 public GameObject GiveButton2;
 public GameObject GiveButton3;
 public GameObject GiveButton4;
 public GameObject GiveButton5;
 public GameObject GiveButton6;
 public GameObject GiveButton7;

 public GameObject GiveText1;
 public GameObject GiveText2;
 public GameObject GiveText3;
 public GameObject GiveText4;
 public GameObject GiveText5;
 public GameObject GiveText6;
 public GameObject GiveText7;


 public GameObject TaskingText1;
 public GameObject TaskingText2;
 public GameObject TaskingText3;
 public GameObject TaskingText4;
 public GameObject TaskingText5;
 public GameObject TaskingText6;
 public GameObject TaskingText7;
 

 public void GiveTask()
 {
     if(MatchText.Match >= 2)
     {
         GiveText1.SetActive(false);
         TaskingText1.SetActive(true);
         Destroy(GiveButton1);

         CoinText.Coin += 5;
         PlayerPrefs.SetInt("Coins", CoinText.Coin);

         PlayerPrefs.Save();
     }
 }

 public void GiveTask2()
 {
     if(MatchText.Match >= 4)
     {
         GiveText2.SetActive(false);
         TaskingText2.SetActive(true);
         Destroy(GiveButton2);

         CoinText.Coin += 10;
         PlayerPrefs.SetInt("Coins", CoinText.Coin);

         PlayerPrefs.Save();
     }
 }

 public void GiveTask3()
 {
     if(MatchText.Match >= 6)
     {
         GiveText3.SetActive(false);
         TaskingText3.SetActive(true);
         Destroy(GiveButton3);

         CoinText.Coin += 10;
         PlayerPrefs.SetInt("Coins", CoinText.Coin);

         PlayerPrefs.Save();
     }
 }

 public void GiveTask4()
 {
     if(MatchText.Match >= 8)
     {
         GiveText4.SetActive(false);
         TaskingText4.SetActive(true);
         Destroy(GiveButton4);
     
         CoinText.Coin += 15;
         PlayerPrefs.SetInt("Coins", CoinText.Coin);

         PlayerPrefs.Save();
     }
 }

 public void GiveTask5()
 {
     if(MatchText.Match >= 10)
     {
         GiveText5.SetActive(false);
         TaskingText5.SetActive(true);
         Destroy(GiveButton5);

         DiamondText.Diamond += 10;
         PlayerPrefs.SetInt("Diamonds", DiamondText.Diamond);

         PlayerPrefs.Save();
     }
 }
     
 public void GiveTask6()
 {
     if(MatchText.Match >= 12)
     {
         GiveText6.SetActive(false);
         TaskingText6.SetActive(true);
         Destroy(GiveButton6);

         CoinText.Coin += 20;
         PlayerPrefs.SetInt("Coins", CoinText.Coin);

         PlayerPrefs.Save();
     }
 }
     
 public void GiveTask7()
 {
     if(MatchText.Match >= 16)
     {
         GiveText7.SetActive(false);
         TaskingText7.SetActive(true);
         Destroy(GiveButton7);

         CoinText.Coin += 30;
         PlayerPrefs.SetInt("Coins", CoinText.Coin);
         
         PlayerPrefs.Save();
     }
 }
Comment
Add comment · Show 1
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 andrew-lukasik · Jan 01 at 01:22 AM 0
Share

This won't answer your question (it's incomplete, either way), but I think you can make this code more concise

 public GameObject[] GiveButton = new GameObject[7];
 public GameObject[] GiveText = new GameObject[7];
 public GameObject[] TaskingText = new GameObject[7];

 void OnEnable ()
 {
     CoinText.Coin = PlayerPrefs.GetInt( "Coins" );
     DiamondText.Diamond = PlayerPrefs.GetInt( "Diamonds" );
 }

 public void GiveTask ( int taskID )
 {
     if( taskID>0 && taskID<_taskConditions.Count )
     {
         if( _taskConditions[taskID]() )
         {
             GiveText[taskID].SetActive( false );
             TaskingText[taskID].SetActive( true );
             Destroy( GiveButton[taskID] );

             _taskRewards[taskID]();

             PlayerPrefs.SetInt( "Coins" , CoinText.Coin );
             PlayerPrefs.SetInt( "Diamonds" , DiamondText.Diamond );
         }
     }
     else Debug.LogError($"{nameof(taskID)} {taskID} is out of range",this);
 }

 Dictionary<int,System.Func<bool>> _taskConditions = new Dictionary<int,System.Func<bool>>(){
     { 0 , () => MatchText.Match >= 1 } ,
     { 1 , () => MatchText.Match >= 2 } ,
     { 2 , () => MatchText.Match >= 4 } ,
     { 3 , () => MatchText.Match >= 6 } ,
     { 4 , () => MatchText.Match >= 8 } ,
     { 5 , () => MatchText.Match >= 10 } ,
     { 6 , () => MatchText.Match >= 12 } ,
     { 7 , () => MatchText.Match >= 16 } ,
 };
 Dictionary<int,System.Action> _taskRewards = new Dictionary<int,System.Action>(){
     { 0 , () => CoinText.Coin += 2 } ,
     { 1 , () => CoinText.Coin += 5 } ,
     { 2 , () => CoinText.Coin += 10 } ,
     { 3 , () => CoinText.Coin += 10 } ,
     { 4 , () => CoinText.Coin += 15 } ,
     { 5 , () => DiamondText.Diamond += 10 } ,
     { 6 , () => CoinText.Coin += 20 } ,
     { 7 , () => CoinText.Coin += 30 } ,
 };

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by henkehedstrom · Jan 07 at 03:59 PM

It looks like you save your data with playerprefs but you don't show where you load the data. When you start your game you could call on PlayerPrefs.GetInt("Coins") to get how many coins you have saved. Something like this should work.

 public void Start()
 {
    Load();
 }
 
 
 public void Load()
 {
   CoinText.Coin = PlayerPrefs.GetInt("Coins");
 }

I don't want to judge your programming but you could do with some refactoring of this code. Instead of having many variables like that you could use arrays. Your GiveTask functions are also very similar, you could probably get by with only one function. I am not sure what MatchText.Match is though so I am not sure how you should refactor it but here is an example of how it could look like with only three variables and one function and almost the same functionality as you have (no checking on the MatchText.Match).

  public GameObject[] GiveButtons;
  public GameObject[] GiveTexts;
  public GameObject[] TaskingTexts;
   
  
  public void GiveTask(int amountOfCoins, int objectIndex)
  {
     GiveTexts[objectIndex].SetActive(false);
     TaskingText[objectIndex].SetActive(true);
     Destroy();
     CoinText.Coin += amountOfCoins;
     PlayerPrefs.SetInt("Coins", CoinText.Coin);
     PlayerPrefs.Save();
  }
  


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 artem_levitin · Jan 15 at 10:31 AM 0
Share

Matching text.Match is the number of games played

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

274 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Dont work Advertisements 0 Answers

error CS0117: `UnityEngine.Time' does not contain a definition for `delaTime' 2 Answers

Still doing Running Animation? 1 Answer

How do I delay an Ad? 2 Answers

UnityEditor.ShaderGraph namespace missing 0 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