I have 10 gameobjects in my project with the same tag. I want to create small app with augmented reality, that shows randomly generated points to check.
So I want to activate 4 (random) GameObjects everytime when app is run.
I know how to create list from the tagged GameObjects, but how to randomly activate only 4 ?
Thank you for your help and support.
PS Please don’t write to search on the forum or web. I’m so depressed… third day 12 hours of work on my code, sitting on the manufacturing plant. and so simply looking function doesn’t work. I wanna die…
You want to make a collection (probably a List) of your 10 gameObjects.
In a loop, which runs 4 times, you want to pick one of these objects at random, activate it, then remove that object from the list so it isn’t selected twice accidentally (and you only end up activating 2/3 objects, instead of 4).
You can choose the object at random by using Random.Range (Unity - Scripting API: Random.Range) and then using floor to make sure it’s an integer rather than a float. Then you can use that number to choose which item from your list you will activate and remove from the list.
So something similar to floor(Random.Range(0, List.Length-1)). You can then use that number to pick and then remove it.
Hope that helps.
[Edit] Oh, FindGameObjectsByTag (or something like that) returns a List I think so that’s how you start off.
static void RandomObjectActive(int ManyToSwitchOn, GameObject[] toSwitchOn)
{
int numberOfObjects = toSwitchOn.Length;
if (ManyToSwitchOn >= numberOfObjects)// if you want to set more online
{
foreach (GameObject someGameObject in toSwitchOn)
{
someGameObject.SetActive(true);
}
}
else
{
Random rnd = new Random();
GameObject[] shufeled =toSwitchOn.OrderBy(a => rnd.Next()).ToArray<GameObject>();
for (int i = 0; i < ManyToSwitchOn; i++)
{
shufeled[i].SetActive(true);
}
foreach (GameObject someGameObject in toSwitchOn)
{
GameObject switched = shufeled.First(g => g.tag == someGameObject.tag);
someGameObject.SetActive(switched.activeSelf);
}
}
}
class GameObject
{
public bool activeSelf = false;
public string tag;
public void SetActive(bool active)
{
this.activeSelf=active;
}
}
And I have this error: ‘Random’ does not contain a definition for ‘Next’ and no accessible extension method ‘Next’ accepting a first argument of type ‘Random’ could be found (are you missing a using directive or an assembly reference?)
Did you actually even read the link that was provided to the docs on how to use Random? You are using Random.Next, but what you want is Random.Range. This was even written for you by another user in the above post.
Like, if you are going to ask for help please read the replies Everything you need has already been given to you.
Okay, you’re making something simple very complicated. And messy.
First, declare an int for the number of objects you want active e.g. [SerializeField] int numberToActivate.
Second, create a collection of all your objects which you can choose from to activate. I already said how to do this.
Next, run a for loop for the numberToActivate times.
In that for loop you will:
Use the floored result of Random.Range to choose an object from your array/List to activate.
Activate that object
Remove that object from your list, so you don’t choose it again by accident.
If you need to ‘reset’ and do this again, you’d create collection of all the objects there and then use a foreach loop to set them all to inactive, then go back to making a list of them, and using that list in the for loop.
As @MadeFromPolygons_1 said, you have not read what I wrote before. You didn’t use Random.Range, you didn’t use FindObjectsByTag or similar to make a collection.
I have no idea why your function has been made static.
I would put this script on a gameController-type object.