Search Unity

Why cant I change playerprefs value? PlayerPrefs.SetFloat("goodrandomvalue", .5);

Discussion in 'Scripting' started by 3, Mar 24, 2013.

  1. 3

    3

    Joined:
    Sep 16, 2012
    Posts:
    387
    Well most of my script is working well, however the

    Code (csharp):
    1. if (PlayerPrefs.GetFloat("goodrandomvalue") == .7){
    2.     PlayerPrefs.SetFloat("goodrandomvalue", .5);
    3.         Invoke("close1", .1);
    4. boughttext.animation.Play();
    5. boughttext.audio.Play();
    6.     }
    7.  
    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:

    Code (csharp):
    1.                      if(hit.transform.name == "Buy upgrade"  goodballupgrade1.gameObject.active == true  PlayerPrefs.GetInt("Money") > 499){
    2. PlayerPrefs.SetInt("Money",PlayerPrefs.GetInt("Money") - 500);
    3.     print (PlayerPrefs.GetInt("Money"));
    4.     if (PlayerPrefs.GetFloat("goodrandomvalue") == .7){
    5.         PlayerPrefs.SetFloat("goodrandomvalue", .6);
    6.             Invoke("close1", .1);
    7.     boughttext.animation.Play();
    8.     boughttext.audio.Play();
    9.         }
    10.             if (PlayerPrefs.GetFloat("goodrandomvalue") == .7){
    11.         PlayerPrefs.SetFloat("goodrandomvalue", .55);
    12.             Invoke("close1", .1);
    13.     boughttext.animation.Play();
    14.     boughttext.audio.Play();
    15.         }
    16.            if (PlayerPrefs.GetFloat("goodrandomvalue") == .7){
    17.         PlayerPrefs.SetFloat("goodrandomvalue", .5);
    18.             Invoke("close1", .1);
    19.     boughttext.animation.Play();
    20.     boughttext.audio.Play();
    21.         }
    22.            if (PlayerPrefs.GetFloat("goodrandomvalue") == .5){
    23. nomoney.animation.Play();
    24. nomoney.audio.Play();
    25. Invoke("close1", .2);
    26. PlayerPrefs.SetInt("Money",PlayerPrefs.GetInt("Money") + 500);
    27.         }
    28.                      }
    29. if(hit.transform.name == "Cancel upgrade"){
    30.                      Invoke("close1", .25);
    31. }
    32. if(hit.transform.name == "Clear all"){
    33. PlayerPrefs.DeleteAll();
    34. }
    35.         }
    Thanks again, and here is the entire script:

    Code (csharp):
    1. private var hit : RaycastHit;
    2. private var ray : Ray;//ray we create when we touch the screen
    3. var clone1 : SpawnBall1;
    4. var boughttext : GameObject;
    5. var buysell : GameObject;
    6. var behindobjects : GameObject;
    7. var buy : GameObject;
    8. var cancel : GameObject;
    9. var nomoney : GameObject;
    10. var textmoney : TextMesh;
    11. var goldballupgrade1 : GameObject;
    12. var goodballupgrade1 : GameObject;
    13. //play boughtext animation and sound when item is bought.
    14. var target1 : ballmade;
    15.   function FixedUpdate () {
    16. textmoney.text = "Money: " + PlayerPrefs.GetInt("Money");
    17.  // PlayerPrefs.SetInt("Money",PlayerPrefs.GetInt("Money") + 1000);
    18.     if(iPhoneInput.touchCount == 1) {
    19.         ray = Camera.main.ScreenPointToRay(iPhoneInput.touches[0].position);
    20.         Debug.DrawLine(ray.origin,ray.direction * 10);
    21.         if(Physics.Raycast(ray.origin, ray.direction * 10,hit)){
    22.  Debug.Log(hit.transform.name);//Object you touched
    23.                    //target1 = GameObject.Find("Ball Made").GetComponent(ballmade);
    24.                    //clone1 = GameObject.Find("Cylinder Spawn 3").GetComponent(SpawnBall1);
    25.                                          }
    26.                                          if(hit.transform.name == "Menureturn"){
    27.                             Application.LoadLevel("LoadMenu");
    28.         }
    29.  
    30.                      if(hit.transform.name == "greatball Upgrade"){
    31.                      Invoke("open1", .15);
    32.                      }
    33.                                           if(hit.transform.name == "goodball upgrade"){
    34.                      Invoke("open2", .15);
    35.                      }
    36.  
    37.                         if(hit.transform.name == "Buy upgrade"  goldballupgrade1.gameObject.active == true  PlayerPrefs.GetInt("Money") > 999){
    38. // Upgrade goldball and take away money if it hasn't been upgraded more than twice, also do purchase animation
    39. //add audio for everything here
    40.                      }
    41.                          if(hit.transform.name == "Buy upgrade"  goldballupgrade1.gameObject.active == true  PlayerPrefs.GetInt("Money") < 999){
    42. nomoney.animation.Play();
    43. nomoney.audio.Play();
    44. Invoke("close1", .2);
    45.                      }
    46.                                               if(hit.transform.name == "Buy upgrade"  goodballupgrade1.gameObject.active == true  PlayerPrefs.GetInt("Money") < 499){
    47. nomoney.animation.Play();
    48. nomoney.audio.Play();
    49. Invoke("close1", .2);
    50.                      }
    51.                      if(hit.transform.name == "Buy upgrade"  goodballupgrade1.gameObject.active == true  PlayerPrefs.GetInt("Money") > 499){
    52. PlayerPrefs.SetInt("Money",PlayerPrefs.GetInt("Money") - 500);
    53.     print (PlayerPrefs.GetInt("Money"));
    54.     if (PlayerPrefs.GetFloat("goodrandomvalue") == .7){
    55.         PlayerPrefs.SetFloat("goodrandomvalue", .6);
    56.             Invoke("close1", .1);
    57.     boughttext.animation.Play();
    58.     boughttext.audio.Play();
    59.         }
    60.             if (PlayerPrefs.GetFloat("goodrandomvalue") == .7){
    61.         PlayerPrefs.SetFloat("goodrandomvalue", .55);
    62.             Invoke("close1", .1);
    63.     boughttext.animation.Play();
    64.     boughttext.audio.Play();
    65.         }
    66.            if (PlayerPrefs.GetFloat("goodrandomvalue") == .7){
    67.         PlayerPrefs.SetFloat("goodrandomvalue", .5);
    68.             Invoke("close1", .1);
    69.     boughttext.animation.Play();
    70.     boughttext.audio.Play();
    71.         }
    72.            if (PlayerPrefs.GetFloat("goodrandomvalue") == .5){
    73. nomoney.animation.Play();
    74. nomoney.audio.Play();
    75. Invoke("close1", .2);
    76. PlayerPrefs.SetInt("Money",PlayerPrefs.GetInt("Money") + 500);
    77.         }
    78.                      }
    79. if(hit.transform.name == "Cancel upgrade"){
    80.                      Invoke("close1", .25);
    81. }
    82. if(hit.transform.name == "Clear all"){
    83. PlayerPrefs.DeleteAll();
    84. }
    85.         }
    86. }
    87.  
    88. function open1 () {
    89. buysell.gameObject.active = true;
    90.                      goldballupgrade1.gameObject.active = true;
    91.                      behindobjects.gameObject.active = false;
    92. }
    93.  
    94. function open2 () {
    95. buysell.gameObject.active = true;
    96.                      goodballupgrade1.gameObject.active = true;
    97.                      behindobjects.gameObject.active = false;
    98. }
    99.  
    100. function close1 () {
    101. buysell.gameObject.active = false;
    102. goldballupgrade1.gameObject.active = false;
    103. goodballupgrade1.gameObject.active = false;
    104.                      behindobjects.gameObject.active = true;
    105. }
    106.  
    107. function nomoney1 () {
    108. buysell.gameObject.active = true;
    109.                      goldballupgrade1.gameObject.active = true;
    110.                      behindobjects.gameObject.active = false;
    111. }
     
  2. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,619
    Using the equality operator (==) on floats is almost never a good idea.

    Floats are stored as approximations of numbers, so exact equality often doesn't work. For instance, the test 7.0 + 7.0 == 14.0 may return false, because it may actually test for something like 6.9999999999 + 6.9999999999 == 14.000000001 behind the scenes.

    This kind of thing is what Mathf.Approximately(...) is for, so look that up in the scripting reference for starters.

    But furthermore, generally a better strategy with floats is to split up the range you're looking at by testing for inequality (greater than or less than), rather than test for specific values within it.
     
  3. 3

    3

    Joined:
    Sep 16, 2012
    Posts:
    387
    Thanks a ton, I figured it out and used:
    (PlayerPrefs.GetFloat("goodrandomvalue") - .7) < 0.00001
    instead.
     
  4. Jaimi

    Jaimi

    Joined:
    Jan 10, 2009
    Posts:
    6,208
    There's built in functionality for approximation, if you want it to look a little nicer:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class example : MonoBehaviour {
    6.     void Example() {
    7.         if (Mathf.Approximately(1.0F, 10.0F / 10.0F))
    8.             print("same");
    9.        
    10.     }
    11. }
    12.