Search Unity

System of random items

Discussion in 'Scripting' started by lucked, Apr 6, 2011.

  1. lucked

    lucked

    Joined:
    Jul 11, 2010
    Posts:
    111
    Hello again I'm doing the racing game and am trying to do on my map to pick up items but I want mariokart style that I picked up a box and inside it was the system of random selection I've ever even imagine how I can do that, I'm wanting to do an arraylist and my items will be on them and send randonizar this list to get a random value, could someone help me please. Thanks I await answers.

    Obs:I am using C#.

    Thanks.
     
  2. Marrrk

    Marrrk

    Joined:
    Mar 21, 2011
    Posts:
    1,032
    Something like this will work for you.

    Code (csharp):
    1.  
    2. Random random = new Random();
    3. var myItem = myItemArray[random.Next(myItemArray.Length)];
    When you need a percentage chance for every item, there are different approaches. For example:
    http://www.vcskicks.com/random-element.php
     
    Last edited: Apr 6, 2011
  3. lucked

    lucked

    Joined:
    Jul 11, 2010
    Posts:
    111
    Thanks Marrrk. Now enjoy it as I could make the system of positions, for example display on my screen that I'm in first, second or third place could help me please?
    Thanks.
     
  4. lucked

    lucked

    Joined:
    Jul 11, 2010
    Posts:
    111
    Marrrk. Desculpe mas o seu código não funcionou e eu precisava dele em C# se poder coloca variavel para mim tambem pois não estou sabendo como declarar em C# uma array ja olhei na documentação da unity mas lá só mostro como é a declaração em javascript poderia por favor me ajudar?
     
  5. Marrrk

    Marrrk

    Joined:
    Mar 21, 2011
    Posts:
    1,032
    To your last post (I hope i did understand it, I dont speak any portuguese):

    You dont need an array if you want to dynamically add items into the list of available items (also its eadier to explain everything with a list).

    Declare and define a List of Items;

    Code (csharp):
    1. List<UsableItem> myItems = new List<UsableItem>();
    2. myItems.Add(new HomingMissile());
    3. myItems.Add(new Missile());
    4. myItems.Add(new Shield());
    5. myItems.Add(new Booster());
    6. myItems.Add(new SuperBoost());
    If you want this as an Array:

    Code (csharp):
    1. UsableItem[] myItems = UsableItem[]
    2. {
    3.   new HomingMissile(),
    4.   new Missile(),
    5.   new Shield(),
    6.   new Booster(),
    7.   new SuperBoost()
    8. };
    Alternative:

    Code (csharp):
    1. UsableItem[] myItems = UsableItem[5];
    2. myItems[0] = new HomingMissile();
    3. myItems[1] = new Missile();
    4. myItems[2] = new Shield();
    5. myItems[3] = new Booster();
    6. myItems[4] = new SuperBoost();
    7.  
    Everything else in my code should be usable as it is.

    To your first post:

    I would nsuggest that you look into the GUI classes and simply display a texture with the position of the player according to the current placing of the player. Please elaborate your problem.
     
  6. lucked

    lucked

    Joined:
    Jul 11, 2010
    Posts:
    111
    Sorry Marrrk, I am Brazilian and I ended up writing in Portuguese my problem that was up there but what I was trying to say is that I needed the example in C # and was not finding a good example in the documentation of unity, but had imagined that to was equal to an ArrayList as it is in java. But thanks! =)