PlayerPrefs not working ? c# (116612)

Okay so -

playerStrength = Random.Range (1, 6);
PlayerPrefs.SetInt ("strength", playerStrength);

works.

In a seperate script in the same project/different scene.

PlayerPrefs.GetInt("strength");

does not work.

It says Parser Error, '(, is wrong.

Also then when I put

Shamwow = PlayerPrefs.GetInt("strength"); 

it tells me that = isn’t allowed.
Quite confused as this is the same as the example here;

and here

http://cgcookie.com/unity/2012/09/10/playerprefs/

Surely something else causes the trouble. If you could show you whole script and the exact compile error it would be easier to help :)

Parser Error; '(, is not defined in class, struct or declaration. It's the PlayerPrefs.GetInt("strength"); that's showing errors (even without Shamwow =) this is the code erroring, everything else works fine.

But this has nothing wrong with it. It should be something trivial syntax error. But the error is not with this line believe me :)

Like @dsada is saying, this is not an error specific to that method. You likely just have a missing/unclosed parenthesis or semicolon or something somewhere earlier in the script.

What the others said. When you make a typo somewhere, the parser sometimes starts thinking it's a whole different type of code. The result is odd errors because it thinks you started/ended an if statement/function/switch/mathematical operation somewhere you didn't.

1 Answer

1

did you remember to SAVE the prefs after setting the int?
any way, like @orb said, it does look like typo somewhere in the script…

It is not neccessary to save them until the end of the program. If you set an entry and then immediately read it then it wont be read from the disk.