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 01:40 AM · instantiatevariablerandomplayerprefs

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.

Comment
Add comment
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

1 Reply

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

Answer by robertbu · Mar 24, 2013 at 02:02 AM

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;

Comment
Add comment · Show 5 · 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
avatar image 3 · Mar 24, 2013 at 02:18 AM 0
Share

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,

avatar image robertbu · Mar 24, 2013 at 02:34 AM 0
Share

If you only want to set it once, the use PlayerPrefs.Has$$anonymous$$ey(). So your code would look something like:

 if (!PlayerPrefs.Has$$anonymous$$ey("goodrandomvalue")){
     PlayerPrefs.SetFloat("goodrandomvalue", .6); }

Note the '!' symbol in the 'if' statement.

avatar image 3 · Mar 24, 2013 at 02:36 AM 0
Share

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,

avatar image robertbu · Mar 24, 2013 at 04:35 AM 0
Share

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.

avatar image 3 · Mar 24, 2013 at 04:57 AM 0
Share

Thanks yet again,

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

10 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

Related Questions

Spawn Random Enemy 3 Answers

For Loop isn't working properly! 1 Answer

Creating a light in game C# 1 Answer

Random.value sometimes doesn't instantiate my prefab(pic included) 1 Answer

instantiating vertically 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