- Home /
How do I make an array from text objects and get them to instantiate randomly?
I have a questionnaire section of my game for a research project. I need to have different question categories and have the questions appear in random orders so there is no pattern. I'm struggling on getting the best way to have multiple questions within different categories and having them instantiate in a random order. I then need to link specific answers to these questions so I can relay data to an external database. I have worked using arrays before and have made prefabs of my text mesh objects, can I just use make arrays that correspond with the categories of questions and put the text prefabs in as gameObjects? Thanks.
Answer by I_Am_Err00r · Jul 09, 2019 at 04:33 PM
I don't have a complier that can verify how well this code works, my apologies, but this should do the trick: (found it here: https://stackoverflow.com/questions/22737687/how-to-create-an-array-of-non-repeating-random-numbers)
public class Example : Monobehaviour
{
public string[] answers;
int[] numberHolder; //array to be filled with numbers up to an
//amount entered by the user eg 42 Max
int[] drawHolder; //array to hold the each random number
//drawn from the pool of numbers eg 7 numbers
public void ShowNumbers()
{
DrawNumbers(answers.length)
}
public String DrawNumbers(int numAmount)
{
Random RandomNumber = new Random();
for (int i = 0; i < numAmount; i++) //loop to fill up numberHolder array
// with predefined limit of numbers
{
numberHolder[i] = i++;
}
for (int i = 0; i < numAmount; i++)
{
// code to pick unique random numbers no greater than numAmount
// and add them to the drawHolder[] array
drawHolder[i] = RandomNumber.Next(1, numAmount);
}
//return the drawHolder array to String
return null;
}
}
Your answer
Follow this Question
Related Questions
How to make sounds louder if I go faster 1 Answer
Make an array of scripts? 1 Answer
Can someone help me with my question with IENumerators in C#? 1 Answer
How can I make this GameObjects array remain permanent when i activate? 1 Answer
Unity TextMeshPro UGUI object is hidden behind other sprites 0 Answers