Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Simple way of randomizing from 3 different groups

Discussion in 'Scripting' started by jessee03, Dec 12, 2014.

  1. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    729
    I'm looking to spawn at random from 3 different groups, as long as a group has at least 1 left. Which would keep repeating until all the groups are equal to zero. I know it's probably really simple but I may be complicating it in my head.

    Code (csharp):
    1.  
    2. var group1 : int = 5;
    3. var group2 : int = 5;
    4. var group3 : int = 5;
    5.  
    6. function chooseRandom () {
    7.  
    8.     while(group1 > 5 || group2 > 5 || group3 > 5) {
    9.      
    10.         yield WaitForSeconds(1);
    11.  
    12.         //choose a group at random and --; from it as long as it's greater than zero
    13.         //Also depending on the group chosen call a specific if statement
    14.         //( example: group1 = call function1(); and group1--; group2 = call function2(); and --group2;
    15.     }
    16. }
    17.  
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    One thing for starters, is put the groups in an int array. Then they can be mathematically dealt with using a random statement. For the function calls, you can use a switch statement.
     
  3. fox4snce

    fox4snce

    Joined:
    Jan 25, 2014
    Posts:
    74
    Pretty much what he said.. except I use c# and I'd make them into classes, then load the classes into lists.

    You have a bunch of lists now... you use List.Add() to add groups to a list, List.Remove() to remove them... then Randomly select one of the lists that has > 0 groups, then randomly select from those groups.

    Set up some groups:
    List1.Add(group1);
    List2.Add(group3);
    etc...

    Now choose one:
    if List1 isn't empty
    whichGroup = Random.Range(0, List1.Count - 1)
    Spawn(whichGroup)
    List1.Remove(whichGroup)
    if all groups are empty, stop

    Hope that helps :)
     
  4. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    729
    I haven't been programming for a lot of the year so I'm a little off. Yes an int array and a switch statement is defiantly something to consider using!
     
    Last edited: Dec 13, 2014
  5. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    729
    It's kind of funny cause while I was walking around my place today I was thinking of using lists before I checked back here. Seems like a perfect combination with a Switch statement to get the job done. Since I can just toss all the variables into the pool of the list and randomize which ones to select from the pool. Whichever number is selected would cause the switch statement to subtract from the group number selected and spawn what I need to spawn. Thank you :) I've been a little rusty programming lately lol.