GetFloat
Gets the float value that corresponds to key in the player preferences.
Read time 2 minutesLast updated 4 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
GetFloat(string, float)
Gets the float value that corresponds to in the player preferences.
keypublic static float GetFloat(string key, float defaultValue)
Parameters
Returns
Type | Description |
|---|---|
| float | The float value corresponding to the given |
Remarks
Returns given if no float value is found for the given .
defaultValuekeyExamples
//Use this script to fetch the PlayerPrefs settings and show them as text on the screen.using UnityEngine;public class PlayerPrefsGetFloatWithDefaultValueExample : 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 100. m_Health = PlayerPrefs.GetFloat("Health", 100); } 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); }}
GetFloat(string)
Returns the float value that corresponds to in the player preferences.
keypublic static float GetFloat(string key)
Parameters
The key used to retrieve the corresponding float value in the player preferences.
Returns
Type | Description |
|---|---|
| float | The float value corresponding to the given |
Remarks
Returns if no float value is found for the given .
0.0fkeyExamples
//Use this script to get the PlayerPrefs settings and display them as text on the screen.using UnityEngine;public class PlayerPrefsGetFloatExample : 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"); } 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); }}