- Home /
Why cant I change playerprefs value?
Well most of my script is working well, however the
if (PlayerPrefs.GetFloat("goodrandomvalue") == .7){
PlayerPrefs.SetFloat("goodrandomvalue", .5);
Invoke("close1", .1);
boughttext.animation.Play();
boughttext.audio.Play();
}
part isn't working. It does take away 500 money, but it doesn't do anything inside that part. It doesn't invoke the close function (which I know is working) and the text animation and audio doesn't play. Any help would be appreciated, I'm in the final stretch of making this app and just as I'm about to finish I'm running into all of these seemingly impossible to solve errors. I assume that it may have to do with the comparison part.
Thanks, here is the part of the script that isn't working:
if(hit.transform.name == "Buy upgrade" && goodballupgrade1.gameObject.active == true && PlayerPrefs.GetInt("Money") > 499){
PlayerPrefs.SetInt("Money",PlayerPrefs.GetInt("Money") - 500);
print (PlayerPrefs.GetInt("Money"));
if (PlayerPrefs.GetFloat("goodrandomvalue") == .7){
PlayerPrefs.SetFloat("goodrandomvalue", .6);
Invoke("close1", .1);
boughttext.animation.Play();
boughttext.audio.Play();
}
if (PlayerPrefs.GetFloat("goodrandomvalue") == .7){
PlayerPrefs.SetFloat("goodrandomvalue", .55);
Invoke("close1", .1);
boughttext.animation.Play();
boughttext.audio.Play();
}
if (PlayerPrefs.GetFloat("goodrandomvalue") == .7){
PlayerPrefs.SetFloat("goodrandomvalue", .5);
Invoke("close1", .1);
boughttext.animation.Play();
boughttext.audio.Play();
}
if (PlayerPrefs.GetFloat("goodrandomvalue") == .5){
nomoney.animation.Play();
nomoney.audio.Play();
Invoke("close1", .2);
PlayerPrefs.SetInt("Money",PlayerPrefs.GetInt("Money") + 500);
}
}
if(hit.transform.name == "Cancel upgrade"){
Invoke("close1", .25);
}
if(hit.transform.name == "Clear all"){
PlayerPrefs.DeleteAll();
}
}
Thanks again, and here is the entire script:
private var hit : RaycastHit;
private var ray : Ray;//ray we create when we touch the screen
var clone1 : SpawnBall1;
var boughttext : GameObject;
var buysell : GameObject;
var behindobjects : GameObject;
var buy : GameObject;
var cancel : GameObject;
var nomoney : GameObject;
var textmoney : TextMesh;
var goldballupgrade1 : GameObject;
var goodballupgrade1 : GameObject;
//play boughtext animation and sound when item is bought.
var target1 : ballmade;
function FixedUpdate () {
textmoney.text = "Money: " + PlayerPrefs.GetInt("Money");
// PlayerPrefs.SetInt("Money",PlayerPrefs.GetInt("Money") + 1000);
if(iPhoneInput.touchCount == 1) {
ray = Camera.main.ScreenPointToRay(iPhoneInput.touches[0].position);
Debug.DrawLine(ray.origin,ray.direction * 10);
if(Physics.Raycast(ray.origin, ray.direction * 10,hit)){
Debug.Log(hit.transform.name);//Object you touched
//target1 = GameObject.Find("Ball Made").GetComponent(ballmade);
//clone1 = GameObject.Find("Cylinder Spawn 3").GetComponent(SpawnBall1);
}
if(hit.transform.name == "Menureturn"){
Application.LoadLevel("LoadMenu");
}
if(hit.transform.name == "greatball Upgrade"){
Invoke("open1", .15);
}
if(hit.transform.name == "goodball upgrade"){
Invoke("open2", .15);
}
if(hit.transform.name == "Buy upgrade" && goldballupgrade1.gameObject.active == true && PlayerPrefs.GetInt("Money") > 999){
// Upgrade goldball and take away money if it hasn't been upgraded more than twice, also do purchase animation
//add audio for everything here
}
if(hit.transform.name == "Buy upgrade" && goldballupgrade1.gameObject.active == true && PlayerPrefs.GetInt("Money") < 999){
nomoney.animation.Play();
nomoney.audio.Play();
Invoke("close1", .2);
}
if(hit.transform.name == "Buy upgrade" && goodballupgrade1.gameObject.active == true && PlayerPrefs.GetInt("Money") < 499){
nomoney.animation.Play();
nomoney.audio.Play();
Invoke("close1", .2);
}
if(hit.transform.name == "Buy upgrade" && goodballupgrade1.gameObject.active == true && PlayerPrefs.GetInt("Money") > 499){
PlayerPrefs.SetInt("Money",PlayerPrefs.GetInt("Money") - 500);
print (PlayerPrefs.GetInt("Money"));
if (PlayerPrefs.GetFloat("goodrandomvalue") == .7){
PlayerPrefs.SetFloat("goodrandomvalue", .6);
Invoke("close1", .1);
boughttext.animation.Play();
boughttext.audio.Play();
}
if (PlayerPrefs.GetFloat("goodrandomvalue") == .7){
PlayerPrefs.SetFloat("goodrandomvalue", .55);
Invoke("close1", .1);
boughttext.animation.Play();
boughttext.audio.Play();
}
if (PlayerPrefs.GetFloat("goodrandomvalue") == .7){
PlayerPrefs.SetFloat("goodrandomvalue", .5);
Invoke("close1", .1);
boughttext.animation.Play();
boughttext.audio.Play();
}
if (PlayerPrefs.GetFloat("goodrandomvalue") == .5){
nomoney.animation.Play();
nomoney.audio.Play();
Invoke("close1", .2);
PlayerPrefs.SetInt("Money",PlayerPrefs.GetInt("Money") + 500);
}
}
if(hit.transform.name == "Cancel upgrade"){
Invoke("close1", .25);
}
if(hit.transform.name == "Clear all"){
PlayerPrefs.DeleteAll();
}
}
}
function open1 () {
buysell.gameObject.active = true;
goldballupgrade1.gameObject.active = true;
behindobjects.gameObject.active = false;
}
function open2 () {
buysell.gameObject.active = true;
goodballupgrade1.gameObject.active = true;
behindobjects.gameObject.active = false;
}
function close1 () {
buysell.gameObject.active = false;
goldballupgrade1.gameObject.active = false;
goodballupgrade1.gameObject.active = false;
behindobjects.gameObject.active = true;
}
function nomoney1 () {
buysell.gameObject.active = true;
goldballupgrade1.gameObject.active = true;
behindobjects.gameObject.active = false;
}
aw man all that duplicate code
all my programmer hairs are standing on end trying to rip themselves out of my body
You do need to call PlayerPrefs.Save to save prefs between sessions. Have you Debug.Log -ed to make sure your code is getting where it's supposed to? If nothing in an if is happening then the conditions probably aren't being met.
Answer by whydoidoit · Mar 24, 2013 at 08:25 AM
You are comparing floats with an == and that is really not a good idea. Floating point rounding issues happen all the time meaning 0.7 is actually 0.700000000000123 or something. You should not test for equality on a float unless its a 0 you are looking for and you set it yourself!
Otherwise you should compare floats like this:
(PlayerPrefs.GetFloat("goodrandomvalue") - .7) < 0.00001
Next up - you should not be using PlayerPrefs like a runtime variable - store stuff in there between games sure, but don't get it out and put it in a variable and use that! PlayerPrefs can be very very slow.
Your answer
Follow this Question
Related Questions
Why is the camera.screenpointtoray off? 1 Answer
Raycasting and foreach loops 1 Answer
how do i fix this small error 2 Answers
Math - calculate position in world space from ray on infinite plane 2 Answers