-2

I am trying to randomly select two items from the list. These items are then set visible in the scene. The problem is, that sometimes it picks the one that has been selected in previous loop.

How do I exclude the first selection?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BeamManager3 : MonoBehaviour {

public List<GameObject> LargeBeamObject = new List<GameObject>();

void Start () {
   LargeBeamPlayerGenerator();
}

void LargeBeamPlayerGenerator(){
    for (int i = 0; i < 2; i++){
        int randomGameObject = Random.Range(0,4);
        GameObject selectedGameObject = LargeBeamObject[randomGameObject];
        MeshRenderer visible = selectedGameObject.GetComponent<MeshRenderer>();
        visible.enabled = true;
    }
}

}
Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111
Tomas
  • 1
  • 1
  • 1
    When picking the second, retry if it picks the same. Or remove the first pick from the choices before picking the second. So many possibilites. – CSharpie Apr 19 '17 at 20:24
  • Remove the first pick from the list, then pick again with the new list length -1 as the second parameter to Random.Range. Is this Unity, btw? Should tag it, if so. – 15ee8f99-57ff-4f92-890c-b56153 Apr 19 '17 at 20:24
  • 1
    This is a duplicate. http://stackoverflow.com/questions/48087/select-n-random-elements-from-a-listt-in-c-sharp – msitt Apr 19 '17 at 20:25
  • You could also randomly reorder the list, then take the first two items. But msitt's solution seems sound, if the commenters on that answer know what they're talking about. – 15ee8f99-57ff-4f92-890c-b56153 Apr 19 '17 at 20:27
  • Possible duplicate of [Select N random elements from a List in C#](http://stackoverflow.com/questions/48087/select-n-random-elements-from-a-listt-in-c-sharp) – Igor Apr 19 '17 at 20:28

3 Answers3

1
var numberOfRandomSelections = 2;

var randomRenderers = Enumerable.Range(0, LargeBeamObject.Count)
       .OrderBy(i => Random.value)
       .Select(i => LargeBeamObject[i].GetComponent<MeshRenderer>())
       .Take(numberOfRandomSelections);

foreach(var renderer in randomRenderers)
   renderer.enabled = true;

Explanation:

  1. Generate numbers for all indexes in objects list
  2. Shuffle indexes
  3. Select mesh renderer for each index
  4. Take only two first renderers
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • Thank you for the reply, yet I was not able to figure out how it works due to my low skills with programming. I am a beginner. I used the solution from Sailesh Chowdary posted above. – Tomas Apr 20 '17 at 09:42
  • @Tomas you do understand that Sailesh solution has bug - it removes objects from `LargeBeamObject` list? – Sergey Berezovskiy Apr 20 '17 at 09:55
  • Yes I found out, but it sort of helped me anyway, and I got a bit further. But you are right it did not solve the hing I was talking about. – Tomas Apr 20 '17 at 15:17
  • @Tomas it didn't solve the problem, but it's still accepted as an answer :) lol BTW why didn't you try the LINQ approach? – Sergey Berezovskiy Apr 20 '17 at 15:35
0

Save data into new temp list and remove the selected element from that list.

void LargeBeamPlayerGenerator(){
List<GameObject> tempObj = LargeBeamObject;
    for (int i = 0; i < 2; i++){
        int randomGameObject = Random.Range(0,4-i);
        GameObject selectedGameObject = tempObj[randomGameObject];
        MeshRenderer visible = selectedGameObject.GetComponent<MeshRenderer>();
        visible.enabled = true;
        tempObj.RemoveAt(randomGameObject);
    }
}
Sailesh Babu Doppalapudi
  • 1,534
  • 1
  • 10
  • 22
0
int last = -1;
int randomGameObject;
for (int i = 0; i < 2; i++){
    while(randomGameObject = Random.Range(0,4) == last) {}
    last = randomGameObject;
    GameObject selectedGameObject = LargeBeamObject[randomGameObject];
    MeshRenderer visible = selectedGameObject.GetComponent<MeshRenderer>();
    visible.enabled = true;
}
paparazzo
  • 44,497
  • 23
  • 105
  • 176