Why is instantiate and playerprefs not working when combined?

So I need to save a variable, called goodrandomvalue so that I can change the chance for spawn of the ball.

So this works:

    var goodball : GameObject;
    var badball : GameObject;
    var goldball : GameObject;
    var startaudio : GameObject;
    var camera1 :GameObject;
    var spawnwait = 2.5;
    var startingtext : GUIText;
    var goodrandomvalue = .6;
    InvokeRepeating("Spawn", 3, spawnwait);
    function Start () {
    yield WaitForSeconds (1);
    startingtext.text = "2";
    startaudio.audio.Play();
    yield WaitForSeconds (1);
    startingtext.text = "1";
        startaudio.audio.Play();
    yield WaitForSeconds (1);
    startingtext.text = "Begin!";
                Handheld.Vibrate ();
        startaudio.audio.Play();
    yield WaitForSeconds (.5);
   // Time.timeScale = 0.1;
    startingtext.text = "";
    }
    function Update () {
 //    spawnwait -= Time.deltaTime * .001;
    }
    
    function Spawn() {
           var goodclone : GameObject;
          var badclone : GameObject;
                    var goldclone : GameObject;
    Debug.Log("New Ball!");
           //Add Random Chance for spawn
                   if ( Random.value > goodrandomvalue) {
             goodclone = Instantiate(goodball, transform.position, transform.rotation);
             goodball.animation.Play("11");
             Destroy (goodclone, 8.5);
             }
             else if (Random.value < .55) {
             badclone = Instantiate(badball, transform.position, transform.rotation);
                        //  Destroy (badclone, 24.5);
                          badclone.name = "badclone";
badclone.transform.tag = "clone";
                          badball.animation.Play("1");  
           }
         
         else if (Random.value > .92) {
         goldclone = Instantiate(goldball, transform.position, transform.rotation);
                                   goldball.animation.Play(); 
                                   goldball.audio.Play();  
                                   camera1.animation.Play("launch");
         }
           
    }

but I need it to save goodrandomvalue, but when I do:

PlayerPrefs.SetInt("goodrandomvalue", .6);

and
if ( Random.value > PlayerPrefs.GetInt(“goodrandomvalue”)) {
Then the goodrandomvalue ball spawns every time. What am I doing wrong?
Sorry for the poor explanation, but thanks anyways.

1 Answer

1

You are passing a floating value to SetInt (which takes integer values), so it is getting truncated to ‘0’; Try:

PlayerPrefs.SetFloat("goodrandomvalue", .6);

Of course you’ll have to use GetFloat() to get the value;

You sir, are my new most favorite person in the world. Thank you for saving me hours of frustration, When I'm a gagilionare, I'll make sure to track you down a give you a million dollars. One more question though, would I have to put if (PlayerPrefs.GetFloat("goodrandomvalue") == 0){ PlayerPrefs.SetFloat("goodrandomvalue", .6); } so it only sets it one time, rather then every time I play the game? Thanks again,

If you only want to set it once, the use [PlayerPrefs.HasKey()][1]. So your code would look something like: if (!PlayerPrefs.HasKey("goodrandomvalue")){ PlayerPrefs.SetFloat("goodrandomvalue", .6); } Note the '!' symbol in the 'if' statement. [1]: http://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.HasKey.html

I could have never figured that out, I looked all over the docs too. There isn't much detail in the Playerprefs part of the docs. But would the way I said it work too? And what If I already made the key, but I wanted to change it one time? Thanks again, sorry for all the questions,

Yes, your way would work. Looking at the script reference, if the key did not exist, it would return a default value of 0.0 (unless you define a different default value). As for "change it one time", you can add new keys, and either leave old one alone or delete them.

Thanks yet again,