Randomly destroy all but one gameobject from an array
So I'm currently working on a hide and seek-esque game and am making it so the every time you play it's hidden in a different spot. The way I went about this was to hide multiple objects and give them the same tag, then to put them all in an array and delete all but one of them. How would you recommend I go about randomly selecting one object in an array and destroying all the others?
Answer by TCMOREIRA · Apr 06, 2020 at 04:04 PM
To do what your asking, literally:
Well it's simple you must only pick a random element from the full array, preferably using the 'Remove' method which returns to you the element which you are removing. Then you keep that element somewhere else for usage. This will give an array with all but that one object.
To actually accomplish what you're trying to do in a simpler manner:
Why don't you simply start with all objects deactivated ('SetActive(false)') then pick a random one and activate just that one? [Deactivate it later when no longer using it, of course]
Your answer
Follow this Question
Related Questions
C# - JS problem. 1 Answer
Enabling & Disabling Script via On Click 1 Answer
i need help jump script c# 1 Answer
Why cant I access the same function twice? 0 Answers
Smoothly multiply float 0 Answers