- Home /
Playerpref float is out of range?
So I am trying to make it so that if the player dies after a certain event, the Game over screen will be slightly different. So I put the following lines of code in the first script when the event happens.
g = 3;
PlayerPrefs.SetFloat("Special Gameover", g);
Then in the other script in the game over screen I put this
public class Gameover : MonoBehaviour {
float g;
void start()
{
g = (PlayerPrefs.GetFloat("Special Gameover"));
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
if (g == 3)
{
print("Active");
}
}
}
}
But whenever I start the game over screen it says: ArgumentOutOfRangeException: Argument is out of range. Parameter name: index
I honestly don't know what I am doing so someone please help.
Are you trying to access an array index somewhere? Show the rest of the code, and the line where you get the error please.
I'm not sure at all and don't have access to a PC right now to try it, but try removing the space in the name (SpecialGameover)
g is a float so you need to add f to the end of the value like...
float g = 3f;
Use int ins$$anonymous$$d
int g = 3;
Answer by KukadiyaPrince · Jun 13, 2017 at 08:07 AM
try this code
public class SetFloat : MonoBehaviour
{
public float g=3;
void Start()
{
PlayerPrefs.SetFloat ("floatValue", g);
TestFloat ();
}
public void TestFloat()
{
float value;
value = PlayerPrefs.GetFloat ("floatValue");
Debug.Log ("Value : " + value);
}
}
This is not relevant to the issue, you just game him the same he already had, the problem is not in the playerpref.