Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by 3 · Mar 24, 2013 at 07:47 AM · iosraycastplayerprefsraysetfloat

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;
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Loius · Mar 24, 2013 at 08:25 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How can I apply damage based on a grenade explosion, but test whether the objects are behind cover so they are not damaged even within the explosion radius? 2 Answers

Math - calculate position in world space from ray on infinite plane 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges