- Home /
Question by
josephcheah · Feb 01, 2021 at 02:44 PM ·
gameobjectarrayarray of gameobjects
How to randomly select multiple gameobjects from array?
Hi guys. I'm a newbie. I want to select multiple game objects from a list of array that i have stored, and no 2 game objects are the same. This is the code that I have written.
void PlaneDisappear()
{
randomGround = GameObject.FindGameObjectsWithTag("Ground");
iOne = Random.Range(0, randomGround.Length);
iTwo = Random.Range(0, randomGround.Length);
while (iOne == iTwo)
{
iTwo = Random.Range(0, randomGround.Length);
}
iThree = Random.Range(0, randomGround.Length);
while (iOne == iTwo || iOne == iThree || iTwo == iThree)
{
iThree = Random.Range(0, randomGround.Length);
}
disappearGroundOne = randomGround[iOne];
disappearGroundTwo = randomGround[iTwo];
disappearGroundThree = randomGround[iThree];
disappearGroundOne.active = false;
disappearGroundTwo.active = false;
disappearGroundThree.active = false;
}
Surely there is an easier and simpler way. Can anyone help? Thanks and appreciate it!
Comment
Answer by unity_ek98vnTRplGj8Q · Feb 01, 2021 at 09:33 PM
randomGround = GameObject.FindGameObjectsWithTag("Ground");
for(int i=0; i<3; i++){
int index = Random.Range(0, randomGround.Length);
randomGround[index].active = false;
randomGround.RemoveAt(index);
}
Your answer
Follow this Question
Related Questions
Public Array of GameObjects 2 Answers
Calculate position in an array of gameObjects 0 Answers
Use an objects (from array) position to focus a camera on 3 Answers
Cannot convert 'UnityEngine.Collider[]' to 'UnityEngine.gameObject[]' using OverlapSphere 3 Answers
Detection if a GameObject is below you or next to you? 1 Answer