- Home /
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.
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?
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;
}
}
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;
}
}
}
in the method TryActivateTheButtonB(); add this:
ButtonB.gameObject.SetActive (false);
and add the static keyword;
private static _buttonIsEnabled;
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.
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.
Your answer
Follow this Question
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