- Home /
Ecosystem generator script generating everything in the same place
Hello, it seems that my ecosystem generator script is "randomly" spawning all of the game objects in about three different places, even though the coordinates are "randomized" every time an object is spawned.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class EcoGeneraterScript : MonoBehaviour {
public GameObject Tree;
public GameObject Mob;
public int NumberOfTree;
public int NumberOfMob;
private float Randomx;
private float Randomz;
void Start () {
Random.InitState (44);
Randomx = Random.Range (50, 950);
Randomz = Random.Range (50, 950);
}
void Update () {
if (NumberOfTree > 0) {
Tree = Instantiate (Tree, new Vector3 (Randomx, 0, Randomz), Quaternion.identity);
NumberOfTree += -1;
Randomx = Random.Range (50, 950);
Randomz = Random.Range (50, 950);
}
if (NumberOfMob > 0) {
Tree = Instantiate (Tree, new Vector3 (Randomx, 0, Randomz), Quaternion.identity);
NumberOfMob += -1;
Randomx = Random.Range (50, 950);
Randomz = Random.Range (50, 950);
}
}
}
I used Debug.Log to tell me what Randomx was, it was equal to the number 137 one-hundred times in a row, It still equals 137 even if I change or remove the InitState.
After I restarted Unity, Randomx was equal to 684 ninety-six times, and 600 four times.
I switched Random.InitState to "Random.seed = System.DateTime.Now.$$anonymous$$illisecond;". I still have the same problem whereas most of the random numbers are the same, although I've noticed that the first random number when I start the game is a truly random number.
Your answer
Follow this Question
Related Questions
Instantiate GameObject on local axis rather than world axis 1 Answer
2D Platform Generation: Spawning Platforms Consecutively 3 Answers
How to randomly instantiate an object at specific locations? 1 Answer
Stopping Instantiated GameObjects overlapping 3 Answers
Random.range multiple instantiations without repetition 1 Answer