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

Problem with playerprefs.

Discussion in 'Scripting' started by MG, Dec 24, 2015.

  1. MG

    MG

    Joined:
    Nov 10, 2012
    Posts:
    190
    I have this code
    Code (CSharp):
    1. void Start () {
    2.         PlayerPrefs.SetFloat ("Bank", 500);
    3.         currentUpgrade = PlayerPrefs.GetInt (item.ToString()+"no");
    4.         Debug.Log ("Check item.ToString: " + item.ToString ());
    5.         Debug.Log ("Check item.ToStringNo: " + item.ToString () + "no");
    6.         Debug.Log ("1: Webcam No. : " + PlayerPrefs.GetInt (item.ToString () + "no"));
    7.         Debug.Log ("2: Webcam No. : " + PlayerPrefs.GetFloat ("Webcamno"));
    8.     }

    This is the output (Check attached file)

    Code (CSharp):
    1. 2: Webcam No. : 1
    2. UnityEngine.Debug:Log(Object)
    3. upgrade:Start() (at Assets/upgrade.cs:30)
    This is what im looking for.
    But im struggling with getting it when i paste the string with the string "no"

    What is wrong
     

    Attached Files:

  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Could you show the code, where you set the player prefs?
     
  3. MG

    MG

    Joined:
    Nov 10, 2012
    Posts:
    190
    The playerprefs is being set in the same code. Here is the full code

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class upgrade : MonoBehaviour {
    6.     public enum Item {
    7.         PC,
    8.         Internet,
    9.         Webcam,
    10.         Monitor,
    11.         Game,
    12.         Music,
    13.         ED
    14.     }
    15.  
    16.     public string UpgradeName;
    17.     public Item item;
    18.     public int AmountsOfUpgrade;
    19.     public int[] price;
    20.     public float[] upgradeStats;
    21.     public int currentUpgrade;
    22.  
    23.     // Use this for initialization
    24.     void Start () {
    25.         PlayerPrefs.SetFloat ("Bank", 500);
    26.         currentUpgrade = PlayerPrefs.GetInt (item.ToString()+"no");
    27.         Debug.Log ("Check item.ToString: " + item.ToString ());
    28.         Debug.Log ("Check item.ToStringNo: " + item.ToString () + "no");
    29.         Debug.Log ("1: Webcam No. : " + PlayerPrefs.GetInt (item.ToString () + "no"));
    30.         Debug.Log ("2: Webcam No. : " + PlayerPrefs.GetFloat ("Webcamno"));
    31.     }
    32.    
    33.     // Update is called once per frame
    34.     void Update () {
    35.         GetComponent<Text>().text = currentUpgrade+" - "+UpgradeName+": $"+price[currentUpgrade];
    36.     }
    37.  
    38.     void BuyUpgrade ()
    39.     {
    40.         if (PlayerPrefs.GetFloat ("Bank") >= price [currentUpgrade]) {
    41.             PlayerPrefs.SetFloat ("Bank", (PlayerPrefs.GetFloat ("Bank") - price [currentUpgrade]));
    42.             currentUpgrade++;
    43.             PlayerPrefs.SetInt(item.ToString()+"no",currentUpgrade);
    44.             PlayerPrefs.SetFloat(item.ToString(),upgradeStats[currentUpgrade]);
    45.  
    46.  
    47.             Debug.Log ("Upgrade Bought");
    48.         } else {
    49.             Debug.Log("Insufficiens Funds");
    50.         }
    51.     }
    52. }
    53.