disable a script c#

hi. i am currently trying to disable a script but it turns out that the code that i am using is not working at all.i got this code from different forums and the code that they suggest is similar to each other and they claim that it is 100% accurate and working.

this is the code

public ShowTheDetails showDetails;
showDetails = GameObject.FindGameObjectWithTag("detailButton").GetComponent();

the purpose of this code to my game is to disable a specific gameObject by disabling the script that is attached into it…can you please help me solve this problem???By the way this is a c# code.

Thank you in advance.

Put the name of your script inside the GetComponent, or drag/drop the name of the target script into showDetails in the Editor (since you made it public) in which case you don’t need the Find

Are you sure it is finding the object properly? As getyour411 said you could just drag and drop it. Also do you want to just disable the script or the gameobject? perhaps you may want to try


That can’t be the full code, because the first line declares a member variable while lines 2 and 3 assign values to that variable outside of a method body. Something is missing from either your paste or from your source.

It can eaisly be fixed by doing this:

public ShowTheDetails showDetails;

void Awake()
    showDetails = GameObject.FindGameObjectWithTag("detailButton").GetComponent<ShowTheDetails>(); 

I’m not saying that it will work right out of the gate, because I don’t have any visibility over your codebase, and whether the ShowTheDetails object is what you’re looking for, or if it is a property or variable of another script that you’d be using GetComponent for.

If not, please consider adding more detail to your questions in the future.

Also, if you want to disable the GameObject, you have to do it a different way. Disabling a script doesn’t disable a gameobject, but the following will: