- Home /
RNG output to a variable + instantiating a prefab based on said variable?
I'm working on a platforming game and what I'd like to do is to create a series of levels which are more or less, random.
What I'm looking for is a script that will choose a variable between a set of numbers, say 1 and 3. Based on the variable selected, it will then instantiate a prefab block or trap on the game object it is attached to. Where I can choose the prefab referenced by each variable on a case by case basis, to prevent getting 10 spike blocks in a row, etc.
I've little javascript experience and I can find RNG scripts that work. I'm just not sure how to output them to a variable and how to reference that variable to instantiate a certain object in my level. I apologize if anything doesn't make any sense, I'm not exactly sure how to phrase a lot of the question. But simply, does anyone have an idea where I can find this script or how I may go about creating it myself?
Answer by Jesse Anders · Nov 09, 2010 at 11:36 PM
To generate a random integer in a specific range, you can use Random.Range(); keep in mind that the number returned will be in the range [min, max). You can then create an array of game objects, assign references to prefabs to the elements of the array, and choose one of the prefabs based on the random number returned.
If you want to ensure that the distribution has particular characteristics (e.g. not getting the same item 10 times in a row), then you may need to use another method to generate the random index. One method would be to apply a 'random shuffle' algorithm to an array of indices with the desired distribution, and then grab indices from this array as needed (you may want to reshuffle the array whenever you get to the end). I don't think the NET libraries include a 'random shuffle' function, but IIRC, andeeee on the forums posted some 'randomization' utility scripts that include such a function.
Well what I was hoping for was a script I could apply to multiple empty game objects which allowed me to pick which prefabs would be referenced for each separate object.
Ex. Write the script and place it on Tile 1. Tile 1 can be a block, trap, or empty. Apply the same script to Tile 2, except Tile 2 can only be a block or a trap, where it's more likely to be a block than a trap(2/3 of the variables reference the block).
You could achieve this by creating a script with a public array of game objects, and then choosing one randomly as I described above. You would then assign references to prefabs to the array elements as desired; to give specific prefabs extra 'weight', you would simply add them more than once. (There are other ways to do it, but that would probably be the most straightforward way.)
Your answer
Follow this Question
Related Questions
Randomly generated number 0 Answers
Random.Range(..) not working 1 Answer
ArgumentException: RandomRangeInt can only be called from the main thread. 1 Answer
Lottery system 1 Answer
Generate 8 unique random integers 2 Answers