- Home /
How to add different random-class objects to different gameobjects?
Setup
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.
Problem
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
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)
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();
Start(){
myRandomGen.seed = 1;
}
}
Anybody knows how to do add different random-class-objects to different gameobjects?
Answer by Demonicdaron · Aug 08, 2015 at 05:50 PM
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;
yourGameObjectInstance.addComponent<propertyToAdd>();
}
}
Your answer
Follow this Question
Related Questions
several Random instances 1 Answer
Having trouble with PlayerPrefs in my script. 1 Answer
How do re-randomize Random.seed after setting it? 3 Answers
Random death instantiate? 1 Answer
Best way to instantiate lots of objects 0 Answers