Hi
i want to make some trophies in my game , i dont know how to code it , but my idea is if the player collected certain object , he can unlock or SetActive a trophy which was hidden , but i want the game to save this , so this unlocked trophy will be active permanently
Hi,
The easiest way to store persistent information is to use: PlayerPrefs. You can use player preferences to store a specific value for your gameobject.
Let’s say your player unlocked a trophy called “Unstoppable”. You’ll store this trophy using the following code:
PlayerPrefs.SetString("trophy", "Unstoppable");
PlayerPrefs.Save();
When you need to retreive this value you use:
string strUnlockedTrophy = PlayerPrefs.GetString("trophy");
Hope it helps!
do i need to use SetActive false and true to use this method or no need ? i want that tophy to be hidden unless the player collect that object , then it becomes Active , what should i add to your code ?
thanks a lot ![]()
You need to use a separate mechanism to enable/disable your game object. PlayerPrefs is a quick solution to store your unlocked trophies.
ok i am doing this mechanism for enabling inactive object
Here is the code for the first class CollectedObject
public class CollectedObject : MonoBehaviour {
void OnTriggerEnter2D(Collider2D collisionObject) {
if (string.Equals (collisionObject.tag, "Bucket")) {
Trophy.flag=true;
Saving();
}
}
public void Saving(){
PlayerPrefs.SetString("trophy", "prize");
PlayerPrefs.Save();
}
and this is the Trophy class
Note: i Disabled the trophy object from he inspector
public class Trophy : MonoBehaviour {
public static bool flag=false;
public GameObject TRP;
public static Trophy GameInstance;
private Trophy ()
{
GameInstance = this;
}
public void Update(){
if (flag= true) {
string strUnlockedTrophy = PlayerPrefs.GetString ("trophy");
TRP.SetActive(true);
}
}
}
Now i have to problems
1- i want to enable that trophy while the player get the collected item, it works fine in my code , but it doesn’t work in seperate scenes , the trophy which i want to enable it is in another scene , so it gives me error
2- i am not sure if this code is saving the unlocked trophy unless the first problem is solved , please tell me if you can help o i can post a new thread
Thanks and Regards ![]()
I did something similar with message status’. I’m had a bool that determine if the message had been delivered or not. When I started the game I set the player prefs for each message to 0 (I used into for the player prefs), at the start of each level I retrieved the player prefs & set the bools based on them (0= false, 1=true). I know you can use strings but this kept it clearer in my head. When the player does whatever is needed I set the bool & also set the playerprefs for that bool. At the end of the level I saved all the playerprefs ready for them to be retrieved at the start of the next level.
Use correct forum area.