Legacy Documentation: Version 2017.2 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

PlayerPrefs.GetFloat

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public static method GetFloat(key: string, defaultValue: float = 0.0F): float;
public static float GetFloat(string key, float defaultValue = 0.0F);

Description

Returns the value corresponding to key in the preference file if it exists.

If it doesn't exist, it will return defaultValue.

print (PlayerPrefs.GetFloat("Player Score"));
//Use this script to fetch the PlayerPrefs settings and show them as text on the screen.

using UnityEngine; using UnityEngine.UI;

public class PlayerPrefsDeleteAllExample : MonoBehaviour { float m_Health;

void Start() { //Fetch the PlayerPref settings SetText(); }

void SetText() { //Fetch the health from the PlayerPrefs (set these Playerprefs elsewhere). If no float of this name exists, the default is 0 m_Health = PlayerPrefs.GetFloat("Health", 0); }

void OnGUI() { //Fetch the PlayerPrefs settings and output them to the screen using Labels GUI.Label(new Rect(50, 90, 200, 30), "Health : " + m_Health); } }

Did you find this page useful? Please give it a rating: