- Home /
Too subjective and argumentative
Pick all gameobject with specific tag
Using GameObject.FindGameObjectsWithTag, how can I set ALL of the gameObjects with a specific tag inactive?
e.g:
if(Input.GetKeyDown(KeyCode.Q))
{
(all gameobjects with tag "PrimaryWeapon").SetActive(false);
}
in C#
someone can help?
Answer by clunk47 · Sep 04, 2013 at 05:04 PM
Here's a C# example of how to use an Array and GameObject.FindGameObjectsWithTag.
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour
{
GameObject[] primaryWeapons;
void Start()
{
primaryWeapons = GameObject.FindGameObjectsWithTag("PrimaryWeapon");
}
void Update()
{
if(Input.GetKeyDown(KeyCode.Q))
{
if(primaryWeapons.Length > 0)
{
foreach(GameObject pw in primaryWeapons)
{
pw.SetActive(false);
}
}
}
}
}
Notice the plural in FindGameObjectsWithTag 'Objects', be sure not to mix this up with 'Object'.
Thanks Clunk47 nice to see people giving information for the lazy or maybe bad google searchers. Not everyone can be as good as the professionals above who only know how to put people down..
i agree with them but for people like myself looking for the same answer im getting tired of searching and finding the same sort of results as this post has, but with no actual answer.. So great to see you working for us(the lamer programmer).
Cant wait for those 2 to get stuck and they run down the same road as asdf123 and get crap thrown there way.. Not attacking just getting tired of same information EVERYWHER$$anonymous$$ There be NO GOOGLE if we didnt place a answer First.
For the record, the google hit was not a unity answer, it was the Unity Documentation page. When you type this question into google, word for word, the first hit is GameObject.FindGameObjectsWithTag It's not like you have to have some kind of super google kung fu, it's literally the first hit.
Also, it's disrespectful to the community to ask a question (and waste their time) that is literally as easy as pasting it into google.
There were actually two questions here--how do I get all the game objects with a tag and then how do I do something with it all. If the OP had posted that he had found FindGameObjectsWithTag and had tried code to use it, but it didn't work, then I would have been glad to answer the question.
I agree to some extent here with @perchik, this is why I edited your question. The google link he posted is legitimate, it links you to the Unity Script Reference page for the first part of your question needed. I won't say you're lazy for it though, I'll just say take the advice that is actually helpful and use it as a future reference, ignore the rest of the unnecessary remarks. Feel free to come back and ask more questions, just try to be as helpful as possible explaining your concerns.
No. Your question was edited to make you look better, the previous comments were because of your original question.
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
How do I find the closest target with a tag? c# 2 Answers
Find all objects that has specific tag 2 Answers
Multiple Cars not working 1 Answer
how to use itween with the object attached to other moving object 0 Answers