- Home /
Player Prefs, Adding Two Variables
Hi I'm using Player Prefs to set two of my variables. I took the code from the Unity Scripting API. While I'm able to set my variable to a specific number using the PlayerPrefs.SetInt function I can't add two variables.
My code is
using UnityEngine;
using System.Collections;
public class League : MonoBehaviour {
int DivisionNumber;
int RacesLeft;
int Points;
int PointsForWin;
int PointsForPrize;
int PointsForOut;
int MoneyForWin;
int MoneyForPrize;
int MoneyForOut;
int FameForWin;
int FameForPrize;
int FameForOut;
Career_Variables career = GameObject.Find("career_Variables").GetComponent<Career_Variables>();
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (RacesLeft >= 0 && Points >= PointsForWin)
{
DivisionNumber --;
PlayerPrefs.SetInt("Career_Variables.Fame", Career_Variables.Fame + FameForWin);
PlayerPrefs.SetInt("Career_Variables.Money", Career_Variables.Money + FameForWin);
}
}
}
This is all I got to in my code before it started generating errors. It shows the errors "error CS1503: Argument #2' cannot convert
object' expression to type int'" error CS1502: The best overloaded method match for
UnityEngine.PlayerPrefs.SetInt(string, int)' has some invalid arguments and error CS1503: Argument #2' cannot convert
object' expression to type `int'.
However when I change it from Career_Variables.Money + MoneyForWin/Career_Variables.Fame + FameForWin those 3 errors go away. This leads me to wonder if you can add two variables with the Player Prefs because I copied and pasted the GetComponent part of the code from another file which showed no error and the gameobject name was the same.
Any help will be appreciated
Thank you in advance
Answer by dsada · Jun 21, 2014 at 08:42 AM
You can not store 2 variables for one key. You need different key for every value. But the error was not about this. The problem was that you argument's type was Object and you have to add Int. that could be easily solved by Convert.ToInt32(argument)
Your answer
Follow this Question
Related Questions
[SOLVED] Help with PlayerPrefs.SetInt 1 Answer
Problem calling variable across scripts (Closed) 1 Answer
'name' is not a member of Object (js) 1 Answer
Is it possible to set variables for classes? 2 Answers
Get Component Error 0 Answers