- Home /
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();
}
}
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 } ,
};
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();
}
Your answer
Follow this Question
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