GameObject red;
// Use this for initialization
void Start () {
red = GameObject.FindGameObjectsWithTag("red");
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.R))
{
red = gameObject.SetActive(true);
green = gameObject.Set Active(false);
}
Hello, probably a simple question but I am trying to get all objects with the tag “green” to be deactivated when R is pressed and red tag will be activated?
Thanks.
First, you should’t equalize red to gameObject.SetActive(true); as that function returns void.
Second, you need an array of GameObjects to save all the tagged objects you are searching for (one for red and one for green). And a loop to active/deactive them.
Your code should be:
public class Red : MonoBehaviour {
GameObject [] red;
GameObject [] green;
// Use this for initialization
void Start () {
red = GameObject.FindGameObjectsWithTag("red");
green = GameObject.FindGameObjectsWithTag("green");
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.R))
{
foreach (GameObject r in red){
r..SetActive(true);
}
foreach (GameObject g in green){
g..SetActive(false);
}
}