- Home /
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();
showDetails.enabled=false;
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.
Answer by getyour411 · Jan 25, 2014 at 02:03 AM
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
Answer by RecursiveRuby · Jan 25, 2014 at 03:40 AM
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
showDetails.gameObject.SetActive(false);
i am sure that it finds the exact script because i have tried to call a method from that script in order to know if it is accessing the desired script. but when i tried to disable it, it doesn't work.
I see you're not specifying a type in GetComponent(), does this even compile? or is this not the code you are actually using? If I wanted to disable the script Foo I would do
GameObject.FindGameObjectWithTag("detailButton").GetComponent<Foo>().enabled=false;
and if I wanted to disable the object itself I would do
GameObject.FindGameObjectWithTag("detailButton").SetActive(false);
I would be surprised if either of these examples don't do what you want. $$anonymous$$eeping in $$anonymous$$d that the former only disables the script and the latter will disable the entire object completely.
GameObject.FindGameObjectWithTag("$$anonymous$$ainCamera").GetComponent().enabled = false;
It works perfectly for me, Thanks Barabicus and happy new year!
Answer by iwaldrop · Jan 25, 2014 at 08:06 PM
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>();
showDetails.enabled=false;
}
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:
showDetails.gameObject.SetActive(false);
I have tried that SetActive(false) but it makes my game object invisible in my screen.All i want is to disable a script so that the functions attached to the game object would be disable but the game object is still visible in my screen.
Then word your question better. You said, "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."