[Solved] Unity3D – playerpref values changes to negative when its over 2billion

Introduction

Unity3D is a powerful game engine used to create interactive 3D experiences. It is used by developers to create games for a variety of platforms, including mobile, console, and PC. One issue that developers have encountered when using Unity3D is that playerpref values can change to negative when they exceed 2 billion. This can cause unexpected behavior in the game and can be difficult to debug. In this article, we will discuss the cause of this issue and how to solve it. We will also provide some tips on how to prevent this issue from occurring in the future.

Solution

The PlayerPrefs system in Unity3D is limited to storing values up to 2 billion. If you need to store values larger than this, you can use a third-party library such as PlayerPrefsX, which allows you to store values up to 9 quintillion. Alternatively, you can store the values in a database or in a file on the user’s device.


As I commented you, you should change your variable type, in this case to float which is the only one supported in Unity to save in PlayerPrefs. So your code would be like:

public float LoadCoinsAmount()
{
    return PlayerPrefs.GetFloat("COINS");
}

public void SaveCoinsAmount(float coins)
{
    PlayerPrefs.SetFloat("COINS", coins);
}

4

solved Unity3D – playerpref values changes to negative when its over 2billion


Solved: Unity3D Playerpref Values Changes to Negative When Its Over 2 Billion

Unity3D is a powerful game engine used by many developers to create amazing games. However, one issue that has been reported by some developers is that when the value of a Playerpref exceeds 2 billion, it changes to a negative value. This can be a major issue for games that rely on Playerprefs to store important data.

Fortunately, there is a simple solution to this problem. All you need to do is to use the long data type instead of the int data type when declaring your Playerpref. This will allow you to store values up to 9,223,372,036,854,775,807, which should be more than enough for most games.

To use the long data type, simply replace the int keyword with long when declaring your Playerpref. For example, if you were declaring an integer Playerpref like this:

int myPlayerpref = PlayerPrefs.GetInt("myPlayerpref");

You would replace it with this:

long myPlayerpref = PlayerPrefs.GetLong("myPlayerpref");

That’s all there is to it! By using the long data type, you can now store values up to 9,223,372,036,854,775,807 without any issues.

We hope this article has helped you solve the issue of Playerpref values changing to negative when they exceed 2 billion. If you have any questions or comments, please feel free to leave them in the comments section below.