I am trying to shuffle 5 buttons (position) from the list using random.range but unable to get all 5 buttons displayed. How to remove duplication in the list? Can any one please help ?
[SerializeField] List<Button> answersButtons = new List<Button>();
[SerializeField] List<Vector2> positions = new List<Vector2>();
void ShuffleAnswersList()
{
for (int i = 0; i < answersButtons.Count; i++)
{
Vector2 tempPosition =
answersButtons*.GetComponent<RectTransform>().position;*
int randomIndex = Random.Range(0, positions.Count);
answersButtons*.transform.position = positions[randomIndex];*
answersButtons[randomIndex].GetComponent().position
= tempPosition;
Debug.Log(randomIndex);
}
}
KevRev
2
You are choosing a random number between 0 and count. This will always give you an index number that doesn’t exist.
Imagine you have a count of 1. You are looping i from 0 to 1, which is actually 2 iterations.
Try this:
int randomIndex = Random.Range(0, positions.Count -1 );