How to add different random-class objects to different gameobjects?


I create chunks each with their own random positioned mountains and trees etc…
At awake of the chunk I set the Random.seed with the chunks position as their seed so every chunk will always be generated in the same way.


I’m using coroutines to spread the quite heavy generation of these chunks but what happens now is that while generating Chunk 1 ( with seed set to 1 ) the next chunk starts to generate and changes the seed to 2 … This messes up the random values for chunk 1.

Possible solutions

  1. make sure each chunk is generated completely before starting with a new chunk (this is not really a viable solution as I really need them to be generated simultaneously)

  2. Add a random-object of class Random to each chunk so they all have their own to call. So when setting the seed it does not affect the other chunks

I’m trying something like this but without any succes:

public class MYCHUNK{

public Random myRandomGen = new Random();

    myRandomGen.seed = 1;

Anybody knows how to do add different random-class-objects to different gameobjects?

Well, to answer your question without considering what you have already done, you could:

  1. create an abstract class to reference all your class-objects as equal classes,
  2. place the scripts you want to use in an array,
  3. and every time you Instantiate a gameobject, you add the class-object to it selecting the class object from the array in a random way.

For example:

//Your parent class will be something like:
abstract public class YourAbstractClass {



//Your child classes will be something like:
public class YourPropertyClass1 : YourAbstractClass {

public class YourPropertyClass2 : YourAbstractClass {

//Your spawner will be something like:
public class YourObjectSpawner: MonoBehaviour {

    public YourAbstractClass[] classObjects_array;
    prublic void InstantiateGameObjectWithRandomClass() {
        int index = Random.Range(0, classObjects_array.GetLength(0)+1)
        YourAbstractClass propertyToAdd = classObjects_array[index];
        GameObject yourObjectInstance = Instantiate(yourObject) as GameObject;