- Home /
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.
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.