Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Bertserker · Nov 18, 2016 at 07:43 AM · c#randomscene-loadingseed

Inconsistent Random Seed on Scene Load

Hey Guys, i'm currently working on a game with randomly generated maps. players should be able to save maps they like. As far as i know the proper way to do this would be to save the Random.seed of the map and then pass it back to Random.InitState() the next time the player loads the map. I try to do this with the following script:

 public class MapSaver: MonoBehaviour {
 
     int currentSeed ;
 
     void Awake () {
         currentSeed = Random.seed;
         if (PlayerPrefs.GetInt("load") > 0)
         {
             PlayerPrefs.SetInt("load", 0);
             Random.InitState(PlayerPrefs.GetInt("save"));
         }
     }
     
     void Update () {
 
         if (Input.GetKeyDown(KeyCode.S))
         {
             PlayerPrefs.SetInt("save", currentSeed);
             Debug.Log("Map saved");
         }
 
         if (Input.GetKeyDown(KeyCode.L))
         {
             PlayerPrefs.SetInt("load", 1);
             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
         }
 
     }
 }

The player can save the current map by pressing S and load it by pressing L. Now what actually happens is that the script saves some other seed than the one actually belonging to the map. So once the player saved a map he gets a completly different map by loading. The loaded map stays the same as long as the seed does not get overwritten. I already set the script to -1000 in the script execution order to prevent other scripts from calling any Random.* functions before the seed gets saved one.

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
0

Answer by Fragmental · Mar 14, 2017 at 05:53 PM

Hey. I think I was having the same problem, basically. I was saving the seed, and the number was always the same, but for some reason, the sequence was different when I loaded a new scene, using the same seed.

Long story short, I believe that may be why Unity devs deprecated Random.seed.

The correct way, now, is to use Random.State.

You can see an example of the correct way to do that here

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Having trouble with PlayerPrefs in my script. 1 Answer

Generate the same 'random' number sequence from a seed 1 Answer

How do I seed the random number generator myself and still get pseudorandom values? 2 Answers

Randomly place a specific number of objects? 1 Answer

Strange random seed issue with level generation 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