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

What is wrong?

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

  1. MG

    MG

    Joined:
    Nov 10, 2012
    Posts:
    190
    I have this code

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerPrefsInfo : MonoBehaviour {
    5.     public float PC;
    6.     public float Internet;
    7.     public float Webcam;
    8.     public float Monitor;
    9.     public float Game;
    10.     public float Music;
    11.     public float ED;
    12.     public float Bank;
    13.     public bool ResetUpgrade = false;
    14.     public bool ResetBank = false;
    15.  
    16.  
    17.     // Use this for initialization
    18.     void Start () {
    19.         if (ResetUpgrade) {
    20.             PC = PlayerPrefs.SetFloat ("PC",0F);
    21.             Internet = PlayerPrefs.SetFloat ("Internet",0F);
    22.             Webcam = PlayerPrefs.SetFloat ("Webcam",0F);
    23.             Monitor = PlayerPrefs.SetFloat ("Monitor",0F);
    24.             Game = PlayerPrefs.SetFloat ("Game",0F);
    25.             Music = PlayerPrefs.SetFloat ("Music",0F);
    26.             ED = PlayerPrefs.SetFloat ("ED",0F);
    27.          
    28.         }
    29.      
    30.         if (ResetBank) {
    31.             Bank = PlayerPrefs.SetFloat ("Bank",0F);
    32.         }
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     void Update () {
    37.  
    38.  
    39.         PC = PlayerPrefs.GetFloat ("PC");
    40.         Internet = PlayerPrefs.GetFloat ("Internet");
    41.         Webcam = PlayerPrefs.GetFloat ("Webcam");
    42.         Monitor = PlayerPrefs.GetFloat ("Monitor");
    43.         Game = PlayerPrefs.GetFloat ("Game");
    44.         Music = PlayerPrefs.GetFloat ("Music");
    45.         ED = PlayerPrefs.GetFloat ("ED");
    46.         Bank = PlayerPrefs.GetFloat ("Bank");
    47.  
    48.  
    49.     }
    50. }
    51.  
    Check attached file for the errors


    SOLUTION

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerPrefsInfo : MonoBehaviour {
    5.     public float PC;
    6.     public float Internet;
    7.     public float Webcam;
    8.     public float Monitor;
    9.     public float Game;
    10.     public float Music;
    11.     public float ED;
    12.     public float Bank;
    13.     public bool ResetUpgrade = false;
    14.     public bool ResetBank = false;
    15.  
    16.  
    17.     // Use this for initialization
    18.     void Start () {
    19.         if (ResetUpgrade) {
    20.             PlayerPrefs.SetFloat ("PC",0F);
    21.             PlayerPrefs.SetFloat ("Internet",0F);
    22.             PlayerPrefs.SetFloat ("Webcam",0F);
    23.             PlayerPrefs.SetFloat ("Monitor",0F);
    24.             PlayerPrefs.SetFloat ("Game",0F);
    25.             PlayerPrefs.SetFloat ("Music",0F);
    26.             PlayerPrefs.SetFloat ("ED",0F);
    27.            
    28.         }
    29.        
    30.         if (ResetBank) {
    31.             PlayerPrefs.SetFloat ("Bank",0F);
    32.         }
    33.     }
    34.    
    35.     // Update is called once per frame
    36.     void Update () {
    37.  
    38.  
    39.         PC = PlayerPrefs.GetFloat ("PC");
    40.         Internet = PlayerPrefs.GetFloat ("Internet");
    41.         Webcam = PlayerPrefs.GetFloat ("Webcam");
    42.         Monitor = PlayerPrefs.GetFloat ("Monitor");
    43.         Game = PlayerPrefs.GetFloat ("Game");
    44.         Music = PlayerPrefs.GetFloat ("Music");
    45.         ED = PlayerPrefs.GetFloat ("ED");
    46.         Bank = PlayerPrefs.GetFloat ("Bank");
    47.  
    48.    
    49.     }
    50. }
    51.  

     

    Attached Files:

    Last edited: Dec 23, 2015
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    What exactly is the point of this post? Getting player prefs constantly in update is even a bad advice as those values should be cached.
     
  3. MG

    MG

    Joined:
    Nov 10, 2012
    Posts:
    190
    The purpose of this is only for dev. I want to be able to check the playerprefs all the time to see where things goes wrong.