- Home /
The question is answered, right answer was accepted
PlayerPrefs does not contain a definition for 'SetInt'
Hi,
I am quite new at this but I can't seem to set an int to the playerprefs. I am using C#. The script uses MonoBehaviour.
In a OnTriggerEnter function i use the following. I've tried the official unity manual examples but I keep getting the same error. PlayerPrefs does not contain a definition for 'SetInt'. Why doesn't it recognize the SetInt (or SetFloat and so on for that matter).
PlayerPrefs.SetInt("crates", 10);
Thanks a bunch!
can you provide more code please? e.g. the full function
Are you defining the UnityEngine namespace like below?
using UnityEngine; using System.Collections;
public class example : $$anonymous$$onoBehaviour { void Example() { PlayerPrefs.SetInt("Player Score", 10); } } If it's ok, maybe you have declared some variable called PlayerPrefs, which could hide the original PlayerPrefs class.
Answer by Rombout · Oct 29, 2012 at 01:22 PM
The problem was that the scriptname and thus the class name was PlayerPrefs. I think a face palm is in order. It was driving me crazy with not even a single Google hit on the literal Unity error. Thanks again, you guys rock!
Solution for the Noobs: rename you own class to PlayerPrefsX (and keep on learning)
p.s. Can, and should, I somewhere put this question to "solved"?
sorry dude, but thats hillarious ^^ but for that there is this forum :)
i don't know how to mark as solved, but it seems, that the solved questions are marked as [Closed] before the question itself.
Another noob here that made the same mistake. The funny part is that it worked once in other script so I moved my set string stuff to a new script named playerprefs and all screwed. loool
Answer by Paulius-Liekis · Oct 29, 2012 at 01:01 PM
Maybe you removed "using UnityEngine;" from the top the the file? or defined class PlayerPrefs in your own source?
I guess, that would have already marked $$anonymous$$onoBehaviour as error. But it's possible.
Defining an own class PlayerPrefs could be the error, but would one not be able to differentiate it by the namespace?
Yes, I think you should be able to access Unity one by UnityEngine.PlayerPrefs
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Differences between mobile save/load data 1 Answer