How to randomly pick value from a list in Unity3D?

shafik

Suppose I have a list of Robot class [List< Robot> myList=new List< Robot>()]. Each Robot has a name and id depending on its colour. Now randomly pick values from the list and give an output of how many Robots of each colour are there on your list. (N.B. Consider you have only 3 colored Robot[Yellow,green, red])

my code:

public class Test : MonoBehaviour
{
    private void Start()
    {
        List<Robot> myList = new List<Robot>();
        List<string> robotList = new List<string>();
        robotList.Add("yellow");
        robotList.Add("green");
        robotList.Add("red");

        int someNum = Random.Range(0, robotList.Count);
        string robotNumber = robotList[someNum];
        robotList.RemoveAt(someNum);
        Robot robot;
        int id = 0;
        robot = new Robot(robotNumber, id);
        Debug.Log(robot);
        id++;
    }
}

public class Robot
{
    public string name;
    public int id;
    public Robot(string name, int id)
    {
        this.name = name;
        this.id = id;
    }
}

but this not work maybe.. actually I don't understand what actually my output is...

Kardux

Not sure to really understand what you're asking for: if it's only about the meaning of the Debug.Log(robot); output, check for @Smartis answer as it answers it perfectly :)

Otherwise, I feel like you wanted to populate a List<Robot> with random picked names. In this case you need to use a loop: Start() method is only called once on start (as its name suggest). If you need to populate a list with random picked colors/names and then display how many of each colors/names are in the list you can do it as follow:

public class Test : MonoBehaviour
{
    private void Start()
    {
        List<Robot> robotsList = new List<Robot>();
        List<string> namesList = new List<string>();
        namesList.Add("yellow");
        namesList.Add("green");
        namesList.Add("red");

        PopulateRobotsList();
        DisplayRobotsListContent();
    }

    private void PopulateRobotsList()
    {
        for(int id = 0; id < 100; id++)
        {
            string robotName = namesList[Random.Range(0, namesList.Count)];
            robotsList.Add(new Robot(robotName, id));
            //Debug.Log(robotsList[robotsList.Count - 1]);
        }
    }

    private void DisplayRobotsListContent()
    {
        int[] robotsNamesCount = new int[namesList.Count];

        for (int i = 0; i < robotsList.Count; i++)
        {
            robotsNamesCount[namesList.IndexOf(robotsList[i].name)] += 1;
        }

        for (int i = 0; i < namesList.Count; i++)
        {
            Debug.Log("Robot(s) named \"" + namesList[i] + "\" : " + robotsNamesCount[i]);
        }
    }
}

public class Robot
{
    public string name;
    public int id;
    public Robot(string name, int id)
    {
        this.name = name;
        this.id = id;
    }
}

Please note I changed some variable names as I found it really hard to understand with the one you provided (ex: robotsList to store the potential colors/names of the robots is a weird choice of name :) ).

Hope this helps,

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pick a value from an array randomly for a particular day

From Dev

Pick a value from an array randomly for a particular day

From Java

How do I pick randomly from an array?

From Dev

How to pick one key from a dictionary randomly

From Dev

SASS: randomly pick background-image from a list

From Dev

How to filter a list and pick randomly one of the resulting records in Excel?

From Dev

Randomly pick variables and give them one value AS3

From Dev

How to randomly pick a string from multiple finals strings in Java?

From Dev

How to randomly pick a number of combinations from all the combinations efficiently

From Dev

How to pick less than N objects randomly from K objects?

From Dev

randomly pick from 4 tables

From Dev

How to have a Game Object remove itself from a list? (Unity3d)

From Dev

How to pick unique pairs from a single list

From Dev

Android - How to pick numbers from a generated list?

From Dev

How to pick data from list in pgsql

From Dev

How to pick a random value from a collection in Scala

From Dev

How to randomly change boolean value in a list

From Dev

How to pick the smallest value in a list when an iterative process is applied to the list

From Dev

Randomly pick elements from a vector of counts

From Dev

Pick a number randomly from two numbers

From Dev

Pick a number randomly from two numbers

From Dev

Android: Randomly pick images from 2 pools

From Java

How to randomly select an item from a list?

From Dev

How to randomly call any method from a list

From Dev

How to test that an element is randomly selected from a list?

From Dev

How to pick randomly UIColor saved in NSArray?

From Dev

How to pick a Javascript object randomly by index?

From Dev

Unity3d: Adding gameobject to List from an array

From Dev

How to get installed apps list with Unity3D?

Related Related

  1. 1

    Pick a value from an array randomly for a particular day

  2. 2

    Pick a value from an array randomly for a particular day

  3. 3

    How do I pick randomly from an array?

  4. 4

    How to pick one key from a dictionary randomly

  5. 5

    SASS: randomly pick background-image from a list

  6. 6

    How to filter a list and pick randomly one of the resulting records in Excel?

  7. 7

    Randomly pick variables and give them one value AS3

  8. 8

    How to randomly pick a string from multiple finals strings in Java?

  9. 9

    How to randomly pick a number of combinations from all the combinations efficiently

  10. 10

    How to pick less than N objects randomly from K objects?

  11. 11

    randomly pick from 4 tables

  12. 12

    How to have a Game Object remove itself from a list? (Unity3d)

  13. 13

    How to pick unique pairs from a single list

  14. 14

    Android - How to pick numbers from a generated list?

  15. 15

    How to pick data from list in pgsql

  16. 16

    How to pick a random value from a collection in Scala

  17. 17

    How to randomly change boolean value in a list

  18. 18

    How to pick the smallest value in a list when an iterative process is applied to the list

  19. 19

    Randomly pick elements from a vector of counts

  20. 20

    Pick a number randomly from two numbers

  21. 21

    Pick a number randomly from two numbers

  22. 22

    Android: Randomly pick images from 2 pools

  23. 23

    How to randomly select an item from a list?

  24. 24

    How to randomly call any method from a list

  25. 25

    How to test that an element is randomly selected from a list?

  26. 26

    How to pick randomly UIColor saved in NSArray?

  27. 27

    How to pick a Javascript object randomly by index?

  28. 28

    Unity3d: Adding gameobject to List from an array

  29. 29

    How to get installed apps list with Unity3D?

HotTag

Archive