- Home /
Trying to make a name lottery.
Hello I am currently trying to make a lucky draw where the a random name is selected to be the lucky winner.
The process is like this :
When the Sort button is selected, it will cycle through the name list (NameList) by deactivating the selected game object and activating the next game object (below) it like a lottery pick. Process repeats until it reaches the bottom of the name list and it resets back to the top.
Pressing the slow button, slows down the speed of the 1st process.
Pressing stop will stop the process and a lucky winner will be chosen.
Picture 1 - The first gameobject in namelist is set active.
Picture 2 - When sort is pressed, the first gameobject (Name1) deactivates and the second one below it (Name1(1)) activates and the cycle repeats until it reaches the last of the name list and it will reset back to the first one (Name1)
Progress : So far I have placed all of the gameobjects in NameList in a List to store them in a data. Now I am stuck on what to do next.
public List<GameObject> nameLists = new List<GameObject>();
public GameObject NameItem;
public int counter;
public NameSort nameSortList;
// Start is called before the first frame update
void Start()
{
NameListGenerator();
}
// Update is called once per frame
void Update()
{
}
public void NameListGenerator()
{
nameLists.Clear();
for(int i = 0; i < counter; i++)
{
NameItem = transform.GetChild(i).gameObject;
nameLists.Add(NameItem);
NameItem = null;
}
}
Thank you.
Answer by sacredgeometry · Mar 05 at 02:18 PM
Personally I wouldnt do it this way (unless there is a specific need). Here is the process I would take:
Put all the names into a collection or array of strings.
Either Randomly sort the collection or create a randomised sequence of the possible indexes and use those as the lookup/index
Do a time delayed iteration through either the randomly sorted collection or the index probably in a coroutine
Use the accessed value to set the text property of a single game object
Have a condition for the coroutine to end after an elapsed time period.
This way you dont have to deal with the visibility states of the current and all other game objects you just have a single text label to deal with. If there is a design reason for this then I would still limit it to the amount visible. i.e if you have a rolling picker and the above and below one are visible then only have 3 not 1 for each item.
Something like:
P.S In this example to slow the rate you would just need to give the intervalTime a slightly wider scope and change it to a larger/ smaller number
P.P.S The stopping action would just need to do the same but for the spinTimeElapsed clause i.e. if you wanted it to be triggered manually and not by a timer
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Input.mousePoition for controllers 0 Answers
Programmatically changing brightness setting on Android using c# 0 Answers
The snail makes a weird behavior when the player push it 1 Answer