Multiple Tags with FindGameObjectsWithTag...?
This is a part of my script:
void Start() {
GameObject[] gems = GameObject.FindGameObjectsWithTag ("blueGem");
foreach (GameObject gem in gems) {
Vector3 pos = gem.transform.position;
pos.y = Random.Range(collectibleMin, collectibleMax);
gem.transform.position = pos;
}
}
How the problem is that in the array it is currently only storing the Blue Gems. I have multiple kinds of gems, like GreenGems, RedGems and DiamondGems. (with tags attached to them 'greenGem', 'redGem', and 'diamondGem')
So now in the game only the position of the Blue Gems are randomized. How can I fix this? ... How can I store all the game in the 'gems' array.
Thanks in advance -- Nathan.
either change the tags to something like "gem" and Find
them or, if you can't do that, get each separately and combine the arrays.
Hey thanks for your reply.
Here's my code now, but I have no idea how to combine the arrays.
using System.Linq
public class BGLooper : $$anonymous$$onoBehaviour {
float collectible$$anonymous$$in = -0.25f;
float collectible$$anonymous$$ax = 0.9f;
void Start() {
GameObject[] bluegems = GameObject.FindGameObjectsWithTag ("blueGem");
GameObject[] greengems = GameObject.FindGameObjectsWithTag ("greenGem");
GameObject[] redgems = GameObject.FindGameObjectsWithTag ("redGem");
GameObject[] diamondgems = GameObject.FindGameObjectsWithTag ("diamondGem");
if(bluegems && greengems && redgems && diamondGems)
{
GameObject [] Gems = bluegems.Concat(greengems).ToArray() + ; // I don't know what to do here...????
}
foreach (GameObject gem in Gems) {
Vector3 pos = gem.transform.position;
pos.y = Random.Range(collectible$$anonymous$$in, collectible$$anonymous$$ax);
gem.transform.position = pos;
}
}
Answer by snowmobeetle · May 19, 2017 at 02:01 PM
Here's what I did in a similar situation (easier to read this way for me - probably not the MOST efficient)
string[] tagsToDisable =
{
"Enemy",
"EnemyAlien",
"EnemyRedAlien",
"ShipThrustEffect"
};
foreach (string tag in tagsToDisable)
{
GameObject[] gameObjects = GameObject.FindGameObjectsWithTag (tag);
foreach (GameObject gameObj in gameObjects)
{
Destroy(gameObj);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612081848im_/https://answers.unity.com/themes/thub/images/avi.jpg)