- Home /
Question by
Klepzy · Jan 27, 2021 at 08:30 PM ·
c#scripting problemscripting beginner
How do you check if a game object is set active using an if statement?
I am trying to check whether or not a game object is set active using an if statement but whenever I try to unity says that I cant do it because it is a "group method" so I am trying to figure out how to do the same thing but using a different method.
Here is the code i have:
public class Pause_controls : MonoBehaviour
{
bool disabled;
public GameObject panelE;
// Start is called before the first frame update
void Start()
{
disabled = false;
}
// Update is called once per frame
void Update()
{
Controls();
if (disabled == true)
{
keybinds();
}
}
void Controls()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
disabled = true;
}
}
void keybinds()
{
if (Input.GetKeyDown("r"))
{
restart();
}
else if (Input.GetKeyDown(KeyCode.Backspace))
{
exit();
}
else if (Input.GetKeyDown("t"))
{
Sc();
}
else if (Input.GetKeyDown("f"))
{
menu();
}
}
void restart() {...
void exit() {...
void Sc() {...
void menu() {...
}
Comment
Best Answer
Answer by CmdrZin · Jan 27, 2021 at 08:50 PM
see https://docs.unity3d.com/ScriptReference/GameObject-activeSelf.html
You could use
if (panelE.activeSelf)
{
Debug.Log("panelE active");
}
else
{
Debug.Log("panelE not active");
}
Answer by Llama_w_2Ls · Jan 27, 2021 at 08:47 PM
If you're using GameObject.SetActive()
, this is a method that sets a gameobject's active state to true or false. The bool for whether it is active in the scene or not is GameObject.activeInHierarchy
. Check doc here