• 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 t$$anonymous$$s 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 (w$$anonymous$$ch 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
avatar image robertbu · Mar 24, 2013 at 02:34 AM 0
Share
avatar image 3 · Mar 24, 2013 at 02:36 AM 0
Share
avatar image robertbu · Mar 24, 2013 at 04:35 AM 0
Share
avatar image 3 · Mar 24, 2013 at 04:57 AM 0
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Instantiate new object from scratch 1 Answer

How to access property of a prefab before Instantiating. 1 Answer

Make sure objects are not instantiated on each other 2 Answers

Changing game object based on variable state? 1 Answer


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