- Home /
How to check If a Component in a GameObject is enabled or disabled
Hi everyone, I've been looking around the internet but I can't seem to find answer to this question. I am trying to use an IF on a Script Component of an GameObject is enabled or disabled.
These code seems to only work for enabling or disabling it
abc.gameObject.GetComponent<defScript>().enabled = true;
abc.gameObject.GetComponent<defScript>().enabled = false;
I've tried all these code, none seems to be working
if (abc.gameObject.GetComponent<defScript>().enabled)
if (!abc.gameObject.GetComponent<defScript>().enabled)
if (abc.gameObject.GetComponent<defScript>().enabled = true)
if (abc.gameObject.GetComponent<defScript>().enabled = false)
if (abc.gameObject.GetComponent<defScript>().enabled == true)
if (abc.gameObject.GetComponent<defScript>().enabled == false)
if (abc.gameObject.GetComponent<defScript>().isActiveAndEnabled)
I would like to emphasize that I am not looking IF the GameObject itself is active/deactivated. So the general solutions wouldn't work.
if (abc.gameObject.GetComponent<defScript>().enabled)
if (!abc.gameObject.GetComponent<defScript>().enabled)
if (abc.gameObject.GetComponent<defScript>().enabled == true)
if (abc.gameObject.GetComponent<defScript>().enabled == false)
Are correct. What problem are you facing ?
You're right. if (abc.gameObject.GetComponent().enabled) if (!abc.gameObject.GetComponent().enabled) After more testing, it actually does work. Thank you!
Answer by StephanCoupland · Jun 16, 2020 at 03:13 PM
I have used the following format to check if a script is enabled on a particular game object:
if (abc.gameObject.GetComponent<defScript>().isActiveAndEnabled == true)