- Home /
PlayerPrefs.GetInt Error MissingMethodException
I have been messing with this for way to long, I'm not sure what the deal is maybe someone can help. Here is my script:
var CurrentXP : int;
var SaveCurrentXP : int;
function Start ()
{
CurrentXP = PlayerPrefs.GetInt("Current XP");
}
function Update ()
{
SaveCurrentXP = CurrentXP;
CurrentXP += 1;
if (Input.GetKeyDown(KeyCode.P))
{
Debug.Log("Current XP: " + CurrentXP + "." + "Saving Current XP");
PlayerPrefs.SetInt("CurrentXP", SaveCurrentXP);
}
}
function OnDisbale()
{
PlayerPrefs.SetInt("Current XP", SaveCurrentXP);
}
Basically for testing purposes I want it to save and load an integer. this interger changes every second, and I can press "P" to save it, also pressing P prints it in the log so I know that its working.
However when I start the game in the editor, I am presented with this error:
MissingMethodException: PlayerPrefs.SetInt
Boo.Lang.Runtime.DynamicDispatching.MethodDispatcherFactory.ProduceExtensionDispatcher ()
Boo.Lang.Runtime.DynamicDispatching.MethodDispatcherFactory.Create ()
Boo.Lang.Runtime.RuntimeServices.DoCreateMethodDispatcher (System.Object target, System.Type targetType, System.String name, System.Object[] args)
Boo.Lang.Runtime.RuntimeServices.CreateMethodDispatcher (System.Object target, System.String name, System.Object[] args)
Boo.Lang.Runtime.RuntimeServices+<Invoke>c__AnonStorey14.<>m__7 ()
Boo.Lang.Runtime.DynamicDispatching.DispatcherCache.Get (Boo.Lang.Runtime.DynamicDispatching.DispatcherKey key, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args)
UnityScript.Lang.UnityRuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args, System.Type scriptBaseType)
PlayerPrefs.Start () (at Assets/Scripts/JavaScript/PlayerPrefs.js:6)
I've tried researching this problem and cannot find anything.
I have successfully used PlayerPrefs in C# (I'm using javascript this time around) on a game I made a while ago. I am looking at it as a reference and nothing is helping.
Any help is greatly appreciated.
Thanks!
Answer by meat5000 · Oct 08, 2013 at 09:46 PM
Some places you put a space in "Current XP" sometimes not. Make sure to get the string exactly right.
Try in your Start() routine:
if(PlayerPrefs.HasKey("CurrentXP"))
{
CurrentXP = PlayerPrefs.GetInt("Current XP");
}
Also I assume you were trying to use OnDisable()
Well I feel really stupid, rena$$anonymous$$g the script from "PlayerPrefs" to "PlayerXP" fixed the problem, apparently you cannot have your script the same name as "PlayerPrefs" because it screws with unity. I was meaning to use OnDisable so thanks for that catch because I didnt even notice that, I appreciate your help a bunch!!
Answer by Tomer-Barkan · Oct 08, 2013 at 09:56 PM
Since JavaScript is not really type safe, you're probably at some point calling the PlayerPrefs.SetInt()
method with a null or incorrect data type, my guess would be SaveCurrentXP
.
Try checking that CurrentXp is an int before running the SetInt()
command:
if (Object.prototype.toString.call(SaveCurrentXP) == Object.prototype.toString.call(1)) {
PlayerPrefs.SetInt("Current XP", SaveCurrentXP);
} else {
Debug.Log("Incorrect data type: " + Object.prototype.toString.call(SaveCurrentXP));
}
Another option would be your typo with "Current XP", in one instance (line 17) you're missing a space between the "Current" and the "XP".
Good luck.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How i make this code in Unity Javascript 2 Answers
using Application.OpenURL for opening new tab? 2 Answers
Vehicle Won't go past 2nd gear. 1 Answer
Network Error 0 Answers