Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Seeking for a solution to our Pick Up System Problem!

Discussion in 'Scripting' started by SP-Designs, Feb 19, 2016.

  1. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    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!
     
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    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.

    http://docs.unity3d.com/ScriptReference/PlayerPrefs.html
     
  3. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    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?
     
  4. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Code (CSharp):
    1. int score = 0;
    2.  
    3. void Start(){
    4.     if(PlayerPrefs.HasKey("SavedScore")){//check if this amount is being stored at all.
    5.         score = PlayerPrefs.GetInt("SavedScore");//set current scored to stored amount.
    6.     }
    7. }
    8. void SaveScore(){
    9.     PlayerPrefs.SetInt("SavedScore", score);//Change the value of the stored amount to the new amount.
    10. }
    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 "F***YouForBeingTooLazyToRead" and it'd still work.
     
    Last edited: Feb 19, 2016
  5. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    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.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ExampleScoreSystem : MonoBehaviour {
    5.  
    6.     // The private reference of the object
    7.     private static ExampleScoreSystem _exampleScoreSystem;
    8.  
    9.     // the score... you can use anything, but score's simple to show you guys
    10.     public float currentScore;
    11.  
    12.     ///
    13.     //  Whatever others variables you'd want here
    14.     ///
    15.  
    16.         // This automatically makes an object if one doesn't automatically exist. Otherwise it'll just pass the reference..
    17.     public static ExampleScoreSystem exampleScoreSystem
    18.     {
    19.         get
    20.         {
    21.             // If there isn't an object yet, make one
    22.             if(_exampleScoreSystem == null)
    23.             {
    24.                 GameObject scoreSystem = new GameObject("ScoreSystem");
    25.                 scoreSystem.AddComponent<ExampleScoreSystem>();
    26.                 _exampleScoreSystem = scoreSystem.GetComponent<ExampleScoreSystem>();
    27.                 DontDestroyOnLoad(scoreSystem);
    28.             }
    29.  
    30.             // Now it's either already made before, or it's now been made.. pass the reference to whatever's asking for the class
    31.             return _exampleScoreSystem;
    32.         }
    33.     }
    34.  
    35.     void OnDestroy()
    36.     {
    37.         // Just to make sure it's not kept when the game closes
    38.         _exampleScoreSystem = null;
    39.     }
    40. }
    41.    
    Code (CSharp):
    1. public class ExamplePickUp : MonoBehaviour
    2. {
    3.     void OnCollisionEnter(Collision collision)
    4.     {
    5.         ExampleScoreSystem.exampleScoreSystem.currentScore += 1;
    6.     }
    7. }
    Code (CSharp):
    1. public class ExampleShop : MonoBehaviour
    2. {
    3.     float playersScoreToSpendAtShop = 0;
    4.  
    5.     void Start()
    6.     {
    7.         playersScoreToSpendAtShop = ExampleScoreSystem.exampleScoreSystem.currentScore;
    8.     }
    9. }
    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).
     
  6. Strategos

    Strategos

    Joined:
    Aug 24, 2012
    Posts:
    255
    You should be aware that player prefs is completely insecure, any values you store there can be easily modified by users.