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 Razputin · Jun 09, 2014 at 04:28 AM · c#randomplayerprefsgenerationseed

Having trouble with PlayerPrefs in my script.

For some reason the variables SeedEntered and WorldCreated return back to 0 when I reload the level. But they should be staying at 1 because of the playerprefs. Does anybody know whats going wrong?

 void OnGUI() {
         
         if(PlayerPrefs.GetInt("Seed Entered") == 0)
         {
             if(GUI.Button(new Rect(10,400,1200,100), "Create the World"))
             {
                 SeedEntered = 1;
                 PlayerPrefs.SetInt("Seed Entered", 1);
                 
             }
         }
     }
     
     void Start()
     {
         
      //PlayerPrefs.DeleteAll();  //Used for testing new keys, comment out this code when not in use. 
 
         if(PlayerPrefs.HasKey("Player Seed"))
         {
             Seed = PlayerPrefs.GetInt("Player Seed");
             
         }
         else
         {
             Seed = Random.Range(0,10000);
             PlayerPrefs.SetInt("Player Seed", Seed); //Sets Player Seed equal to Seed
         } 
         
         
     }
     
     void Update () {
         if(PlayerPrefs.GetInt("Seed Entered") == 1 && PlayerPrefs.GetInt("World Created") == 1)
         {
             Random.seed = Seed;
         }
         else if(PlayerPrefs.GetInt("Seed Entered") == 1 && PlayerPrefs.GetInt("World Created") == 0)
         {
                 for(int z = 0; z < GameHeight; z++)
                 {
                     for(int x = 0 ; x < GameWidth; x++)
                     {
                         float rnd = Random.value;
                         if(rnd <0.25f)
                         {
                             Instantiate(TileOne, new Vector3(x,0,z), Quaternion.identity);
                         }
                         else if(rnd <0.5f)
                         {
                             Instantiate(TileTwo, new Vector3(x,0,z), Quaternion.identity);
                         }
                         else
                         {
                             Instantiate(TileThree, new Vector3(x,0,z), Quaternion.identity);
                         }
                         WorldCreated = 1;
                         PlayerPrefs.SetInt("World Created", 1);
                     }
                 }
             }
 
         }
     }



UPDATE: I did

 Debug.Log(PlayerPrefs.GetInt("Player Seed"));
 Debug.Log (PlayerPrefs.GetInt("Seed Entered"));
 Debug.Log (PlayerPrefs.GetInt("World Created"));

And it says that seed entered and world created are still at 1. And the seed is carrying over too.

So how come it won't run this line of code when I return to the level?

    if(PlayerPrefs.GetInt("Seed Entered") == 1 && PlayerPrefs.GetInt("World Created") == 1)
         {
             Random.seed = Seed;
         }
Comment
Add comment · Show 3
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 Razputin · Jun 09, 2014 at 08:24 PM 0
Share

Bumping post

avatar image Razputin · Jun 10, 2014 at 01:22 AM 0
Share

Bumping post

avatar image Razputin · Jun 12, 2014 at 06:43 AM 0
Share

Bumping post

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by robusto · Jun 09, 2014 at 06:33 AM

You need to Save your changes.

PlayerPrefs.Save();

http://docs.unity3d.com/ScriptReference/PlayerPrefs.Save.html

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 Razputin · Jun 09, 2014 at 03:18 PM 0
Share

Didn't seem to change anything.

avatar image robusto · Jun 14, 2014 at 10:20 AM 0
Share

So is your problem that you aren't generating tiles when you reload the level because "World Created" is set to 1, so that code that generates your tiles never gets executed because i t is saved to 1??? Because if you are reloading your level and your tiles disappear you shouldn't save your "World Created" to 1 in your PlayerPrefs.

avatar image Razputin · Jun 14, 2014 at 07:57 PM 0
Share

but because of

 if(PlayerPrefs.GetInt("Seed Entered") == 1 && PlayerPrefs.GetInt("World Created") == 1)
         {
             Random.seed = Seed;
         }

shouldn't it just make the seed for the level the seed that I saved earlier?

avatar image robusto · Jun 15, 2014 at 12:15 PM 0
Share

So I'm guessing the PlayerPrefs.Save() now saves your values and it carries over. But it sounds as though that didn't fix your problem or it isn't the desired effect you were looking for. Can you describe in full detail what the new problem is?

avatar image Razputin · Jun 16, 2014 at 12:15 AM 0
Share

When I return to the level it is gone. But the values are still saved at 1 and 1, so it should run

 `if(PlayerPrefs.Has$$anonymous$$ey("Player Seed"))
         {
             Seed = PlayerPrefs.GetInt("Player Seed");
             if(PlayerPrefs.GetInt("Seed Entered") == 1 && PlayerPrefs.GetInt("World Created") == 1)
             {
                 Random.seed = Seed;
             }`
 However it clearly isnt.
 

However it clearly isnt.

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Strange random seed issue with level generation 1 Answer

How to save a random seed to a playerpref 2 Answers

How do you get a seed value from UnityEngine.Random.State? 2 Answers

Inconsistent Random Seed on Scene Load 1 Answer

Random Island generation using Perlin Noise ? 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