Angelsm Angelsm - 2 months ago 10
C# Question

Activate and desactivate with same button

I want to create a button to activate and deactivate 3 gameobjects, but I don't get it. This is the script I'm using. Can anybody help me?

using UnityEngine;
using System.Collections;

public class OcultarPlayer : MonoBehaviour {

public GameObject objeto1;
public GameObject objeto2;
public GameObject objeto3;

void OnMouseDown() {
if(objeto1.GetComponent<Renderer>().enabled == true && objeto2.GetComponent<Renderer>().enabled == true && objeto3.GetComponent<Renderer>().enabled == true)
{
objeto1.GetComponent<Renderer>().enabled = false;
objeto2.GetComponent<Renderer>().enabled = false;
objeto3.GetComponent<Renderer>().enabled = false;
}

if (objeto1.GetComponent<Renderer>().enabled = false && objeto2.GetComponent<Renderer>().enabled == false && objeto3.GetComponent<Renderer>().enabled == false)
{
objeto1.GetComponent<Renderer>().enabled = true;
objeto2.GetComponent<Renderer>().enabled = true;
objeto3.GetComponent<Renderer>().enabled = true;
}

}
}

Answer

Do something simpler like this:

public Renderer objeto1;
public Renderer objeto2;
public Renderer objeto3;

bool enabled = false;
void OnMouseDown() 
{
    objeto1.enabled = enabled;
    objeto2.enabled = enabled;
    objeto3.enabled = enabled;
    enabled = !enabled;
}