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 05, 2014 at 04:17 PM · randomplayerprefssavegenerationseed

How to save a random seed to a playerpref

I'm trying to save a random seed to a PlayerPref so the game loads the same world each time, after its been created once first. I know this is wrong, but this is what I have so far.

     public int GameWidth;
     public int GameHeight;
     public int SeedNumber;
 
     public int MapCreated = 0;
     
     // Use this for initialization
     void Start () {
         if(MapCreated != 1)
         {
         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);
             }
             }
         }
             PlayerPrefs.GetInt("MapCreated", 1);
             SeedNumber = Random.Range(1,1000);
         }
     
         if(MapCreated == 1)
     {
             PlayerPrefs.GetInt("Seed", SeedNumber);
             Random.seed = SeedNumber;
     }
     }

I'm trying to do it like this

Create world. Get worlds seed. Save the worlds seed. Use the saved seed from now on when loading the world.

I think most of my problem comes from the fact that I'm not sure how to use PlayerPrefs correctly. If anyone can show or explain how I'd appreciate it.

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

2 Replies

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

Answer by perchik · Jun 05, 2014 at 04:24 PM

Use SetInt to save a value in playerprefs. Think of playerprefs like bank of boxes, where each box has a name. Set* put something in a box and tells the box what it's name is. Get* gets whatever is in the box with the name you supplied.

* String, Int or Float

Comment
Add comment · Show 1 · 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 05, 2014 at 04:32 PM 0
Share

Okay so... I create the world to make the seed. Then I Set the seed to the Playerpref. Then when I want to return to that world I Get the seed from the playerpref.

avatar image
1

Answer by PouletFrit · Jun 05, 2014 at 04:32 PM

You need to check if you have the seed key in PlayerPref, then take decision based on that.

 void Start() {
     // First let's check if the seed exist in player prefs
     if (PlayerPrefs.HasKey("MapRandomSeed")) 
     {
         // If Map Random Seed exist, lets get that value
         int seed = PlayerPrefs.GetInt("MapRandomSeed");
         Random.seed = seed;
         CreateMap();
     }
     else
     {
         // Map Random Seed doesn't exist, so we know that the map wasn't created earlier
         CreateMap();
     }
 }
 
 private void CreateMap(int seed) {
     // Create the map as u were doing, now that we have the appropriate seed

     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);
             }
         }
     }
 
     // Once you created your map save the random seed used
     PlayerPref.SetInt("MapRandomSeed",Random.seed);
     // Don't forgot to save
     PlayerPref.Save();
 }
Comment
Add comment · Show 1 · 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 05, 2014 at 05:15 PM 0
Share

Trying to get this to work now.

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

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

Related Questions

Having trouble with PlayerPrefs in my script. 1 Answer

Generate minecraft alike terrain 0 Answers

Questions about changes to UnityEngine.Random. Random.Seed deprecated 3 Answers

Strange random seed issue with level generation 1 Answer

How do you save a seed? 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