Hello people! I am part of a small team(2 people) and we are making a 2D platformer.We are trying to create a pick up system/script and we are facing some problems.We have already added the basic things so when the player collides with the pickup, he gets 1 point and then it de-activates but if he dies he looses points.We have many levels and the player can replay them as many times as he wants.We are facing a major problem.Our game will have a shop where you can purchase skins and other items with the coins you pick up.We want somehow(maybe with playerprefs), to save the fishes gathered in every level, every time the player plays a level and then add them(the coins) every time to the shop’s total coins text!Can someone help us?Thanks in advance!
So your problem isn’t picking stuff up, but saving the amount picked up. Those are 2 very different problems. Playerprefs should be easy enough to figure out using the “Get…” and “Set…” functions, to quickly get something up and running for now.
Yeah but i tried to figure out how i should use playerprefs for this kind of issue but i could not.Can you give me an example on how it should work?Maybe i should post the two scripts i am using?
int score = 0;
void Start(){
if(PlayerPrefs.HasKey("SavedScore")){//check if this amount is being stored at all.
score = PlayerPrefs.GetInt("SavedScore");//set current scored to stored amount.
}
}
void SaveScore(){
PlayerPrefs.SetInt("SavedScore", score);//Change the value of the stored amount to the new amount.
}
At least give the link a look, because what I wrote here is not different from what’s in the given examples in the API. The string “SavedScore” is a key. You can name it whatever you want. I can call the key “FuckYouForBeingTooLazyToRead” and it’d still work.
It sounds like you might want to consider making an persistent object in the game. This is a basic setup of a ‘Singleton’. A Singleton is a common way of making an object, which will force only one instance of it to be made.
using UnityEngine;
using System.Collections;
public class ExampleScoreSystem : MonoBehaviour {
// The private reference of the object
private static ExampleScoreSystem _exampleScoreSystem;
// the score... you can use anything, but score's simple to show you guys
public float currentScore;
///
// Whatever others variables you'd want here
///
// This automatically makes an object if one doesn't automatically exist. Otherwise it'll just pass the reference..
public static ExampleScoreSystem exampleScoreSystem
{
get
{
// If there isn't an object yet, make one
if(_exampleScoreSystem == null)
{
GameObject scoreSystem = new GameObject("ScoreSystem");
scoreSystem.AddComponent<ExampleScoreSystem>();
_exampleScoreSystem = scoreSystem.GetComponent<ExampleScoreSystem>();
DontDestroyOnLoad(scoreSystem);
}
// Now it's either already made before, or it's now been made.. pass the reference to whatever's asking for the class
return _exampleScoreSystem;
}
}
void OnDestroy()
{
// Just to make sure it's not kept when the game closes
_exampleScoreSystem = null;
}
}
public class ExamplePickUp : MonoBehaviour
{
void OnCollisionEnter(Collision collision)
{
ExampleScoreSystem.exampleScoreSystem.currentScore += 1;
}
}
public class ExampleShop : MonoBehaviour
{
float playersScoreToSpendAtShop = 0;
void Start()
{
playersScoreToSpendAtShop = ExampleScoreSystem.exampleScoreSystem.currentScore;
}
}
With the ExampleScoreSystem, you don’t even need to add it to a GameObject, or put it in any scene… Just start calling it when you need it, and it’ll create itself, and it’ll exist until the game closes (including between scenes).
You should be aware that player prefs is completely insecure, any values you store there can be easily modified by users.