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?
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?
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’.