So i have two scripts on the same gameobject and i want to disable one script from the other. I found some people saying i can use GetComponent(scriptname).enabled = false;
but it doesnt work for me same with GetComponent<scriptname>().enabled = false;
any ideas on why this is happening?
I hope scriptname is the name of the class. It should work.
Alternatively you can just save a reference to the script somewhere and call enabled on that.
(Thats also the recommended that)
This one is correct:
GetComponent<scriptname>().enabled = false;
But it only works if a few things are true:
scriptname
is the class name of the script you’re trying to get. i.e. it looks likepublic class scriptname : MonoBehaviour
- Both the
scriptname
script and the script this code is in need to be attached to the SAME GameObject.
The script i wanna disable has VersionedMonoBehaviour instead of just MonoBehaviour, is that maybe the reason?
Explain what you mean by “doesn’t work”. If you are getting an error, post the error.
What is a VersionedMonoBehaviour?
No that’s most likely not the reason. What exactly is going wrong? We’re not mind readers.
Ok, my bad. So the error when i use GetComponent<name>().enabled = false;
is that it says “The type or namespace name could not be found”. When i use GetComponent(name).enabled = false;
the error is “Component’ does not contain a definition for ‘enabled’ and no accessible extension method ‘enabled’ accepting a first argument of type ‘Component’ could be found (are you missing a using directive or an assembly reference?)”
GetComponent<name>().enabled = false;
You have to replace ‘name’ with the name of your script.