- Home /
Don't Load Collected items
My Problem is that i have coins in the game (all coins are childs in one main EpmtyGameObject Prefab) and the player can collect them. but how can I make it NOT to load them again if the player has collected them,Using PlayerPrefs if possible because I want the user be able to restart the hole game (By clicking the reset game button on the settings of the game,Witch will reset PlayerPrefs) and have these coins back
(some levels have 2 coins some have more than 30)
If you know bit operations, you could save an integer to the playerprefs, where each bit represents the state of a coin (up to 31 bits if I'm correct). When instantiated or activated any other way, the parent could read that int, check each bit for 0 or 1 and activate or deactivate the corresponding coin.
Answer by Mmmpies · Feb 15, 2015 at 05:56 PM
Keep the coins referenced in an array of bools on the player. When you start a new game set all bools to false, when the player picks up a coin set that coins bool to true in the array.
When a level loads only show the coins set to false.
Don't know how many levels you have but you could have an array for each level.
You still need to save to playerprefs but, happily, that's been made a whole lot easier because of this:
Sorry to ask, but how can I use it am totally new with ArrayPrefs2 (first time)
Not used it myself yet, just knew it was there. Appears to be PlayerPrefsX.SetBoolArray but let me test some code so I'm not telling you bad info.
O$$anonymous$$ I'm familiar with C# so I'll show show a brief test I just carried out in C#:
using UnityEngine;
using System.Collections;
public class testPPX : $$anonymous$$onoBehaviour {
private bool[] levelOneCoins;
void Start()
{
levelOneCoins = new bool[5] {true, true, true, true, true};
TESTPPX();
}
void TESTPPX()
{
PlayerPrefsX.SetBoolArray("lvl1coins", levelOneCoins);
Debug.Log (" levelOneCoins 0 = " + PlayerPrefsX.GetBoolArray("lvl1coins")[0]);
}
}
That just saves out to playerPrefsX the 5 values in levelOneCoins. I used the C# PlayerPrefsX from that web page.
Anyway the debug log just looks at the 0 element in that saved array. You can also do:
Debug.Log(PlayerPrefsX.GetBoolArray("lvl1coins").Length);
Which, in this case, is 5.
EDIT
Hmmm, should really show you how to get the values as well:
private bool[] gotLevelOne; // add this at the top
//and add this to the TESTPPX
gotLevelOne = PlayerPrefsX.GetBoolArray("lvl1coins");
Debug.Log("BOOL " +gotLevelOne[0] + " length = " + gotLevelOne.Length);
No problem, if it answered your question you should really tick it as correct. It helps all round, I get a few $$anonymous$$arma points, you don't get a reputation for not ticking. Stops others from trying to answer it and helps people with similar issues.
Get a rep for not ticking and a lot of people will ignore your questions.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
when mouse is on a object do this 2 Answers
press e to speek -1 Answers
Distribute terrain in zones 3 Answers