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 /
avatar image
0
Question by Arcticstar · Jul 19, 2017 at 09:35 PM · unity 5playerprefssave data

How to Save Object (Button) information on game restarting? please check code.

what I'm trying to do here is to set a button active and disable another button By pressing a Button, and Save this action on game Restarting.

I tried this code below but not working.

using UnityEngine; using System.Collections; using UnityEngine.UI; using System;

public class BuySmoke : MonoBehaviour {

 private int i;

 public int Price;

 private int CoinIhave;

 public Button BuYsmoke;

 public GameObject buy;

 void Start () {
 
     i =Convert.ToInt32 (buy);

     CoinIhave = PlayerPrefs.GetInt ("CoinBank");

 }


 public void Buy(){

     if (CoinIhave >= Price) {

         CoinIhave -= Price;
         PlayerPrefs.SetInt ("CoinBank", CoinIhave);

         BuYsmoke.gameObject.SetActive (true);

         gameObject.SetActive (false);


         PlayerPrefs.SetInt ("selected", i);
     }

     if (CoinIhave < Price) {

         Debug.Log ("Not Enough Coin");
     }


 }

}

I tried this method by changing Object to int then using the playerPrefs but it doesn't work.

and yes I have the PlayerPrefs.GetInt("selected") on the maincamera

so please any help or advise even links to tutorial will be appreciated.

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 Trevdevs · Jul 19, 2017 at 10:10 PM 0
Share

What part of the code is not working is the Playerprefs variable not being set, or is the button not disabling and other isn't enabling?

2 Replies

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

Answer by Reynarz · Jul 19, 2017 at 10:30 PM

I like answer in an generic manner, so try to figure out this :D

This is the class of the button that you want desactivate(Disable) after pressing it.

From now i will call it"_buttonB".

  public Button _buttonA; //button that I need activate.
  public Button _buttonB; //button that i need desactivate.

  //If you wanna keep the value of this variable while the game is running put the 'static' keyword.
  private bool _isButtonAEnabled; 
                                                               
  private bool _wasButtonBEnabled = false;

  private void Awake()
  {
      VerifyIfButtonAIsEnabledInTheSaveData();
      TryActivateTheButtonB();
  }

  private void Update()
  {
       TryActivateTheButtonB();
  }

  //this is the "OnClick" event, from your inspector.
  public void OnButtonThatINeedDesactivate()
  {
         if(_isButtonAEnabled)
         {
               DesactivateButton();
               SaveTheDesactivateButtonValue();
         }
  }

   private void VerifyIfButtonAIsEnabledInTheSaveData()
   {
           //I never used playerprefs before, but you can say, 1 is true and 0 is false.
         _isButtonAEnabled = (PlayerPrefs.GetInt("isButtonAEnabled", 1)) == 1? true: false;
   }

   private void DesactivateButton()
   {
        _buttonB.enabled = false;
   }

   private void SaveTheDesactivateButtonValue()
   {
         //Set the value of 0.. (in this case 0 is equal to false)
         PlayerPrefs.SetInt("isButtonAEnabled", 0);
   }

   private void TryActivateTheButtonB()
   {
        if(!_isButtonAEnabled && !_wasButtonBEnabled )
        {
          
           _buttonA.enabled = true;
           _wasButtonBEnabled = true;
        }
    }
Comment
Add comment · Show 4 · 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 Arcticstar · Jul 20, 2017 at 06:18 PM 0
Share

I tried this, It works but like in Half working. when I press the Button the Button I need to deactivate will get back after restarting, and the Button I need to active wont show until I change scene. so it saves the Enabled Button but not the Disabled Button.

here the script I tried copying yours in it.

 public Button ButtonA;
 public Button ButtonB;

 private bool _buttonIsEnabled;
 private bool _buttonIsDisabled = false;

 public int Price;

 private int CoinIhave;


 void Awake() {
     
     VerifyIfButtonAIsEnabledInTheSaveDate ();
     TryActivateTheButtonB ();
 }


 void Start () {
 


     CoinIhave = PlayerPrefs.GetInt ("CoinBank");

 }


 void Update() {

     TryActivateTheButtonB ();

 }


 public void Buy(){

     if (CoinIhave >= Price) {

         CoinIhave -= Price;
         PlayerPrefs.SetInt ("CoinBank", CoinIhave);
     
         if (_buttonIsEnabled) {
             ButtonB.gameObject.SetActive (false);

             SavetheDisactiveValue ();

         }
     }

         if (CoinIhave < Price) {

             Debug.Log ("Not Enough Coin");
         }


 }


     private void VerifyIfButtonAIsEnabledInTheSaveDate () {

         _buttonIsEnabled = (PlayerPrefs.GetInt ("isButtonAEnabled", 1)) == 1 ? true : false;


     }

 private void SavetheDisactiveValue () {

     PlayerPrefs.SetInt ("isButtonAEnabled", 0);

}

 private void TryActivateTheButtonB (){
     if(!_buttonIsEnabled && !_buttonIsDisabled) {

         ButtonA.gameObject.SetActive (true);
         _buttonIsDisabled = true;
     }
 }

}

avatar image Reynarz Arcticstar · Jul 20, 2017 at 06:50 PM 0
Share

in the method TryActivateTheButtonB(); add this:

  ButtonB.gameObject.SetActive (false);

and add the static keyword;

  private static _buttonIsEnabled;
avatar image Arcticstar Reynarz · Jul 20, 2017 at 06:54 PM 0
Share

haha I just replayed that I did that coding thing to solve it, and when I posted comment the page got updated and I saw your comment. thanks.

Show more comments
avatar image
0

Answer by Arcticstar · Jul 20, 2017 at 06:52 PM

NeverMind I solved it,

I just added the ButtonB.gameObject.SetActive(false);

to the void TryActivateTheButtonB()

Now I will try to solve the Button A being shown only when scene changes.

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

135 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

Related Questions

Android Build not deleting PlayerPrefs 1 Answer

How to save color 3 Answers

Binary writer not saving game data on iOS builds 1 Answer

How to communicate between iOS and Unity3D? 0 Answers

How can I collect playerprefs values? 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