- Home /
Disabling a Script with C#
I am trying to create a script disable another script attached to the same GameObject.
I am using the following line:
GetComponent<ScriptName>().enabled = false;
However, I get this error:
error CS0246: The type or namespace name 'ScriptName' could not be found. Are you missing a using directive or an assembly reference?
What am I doing wrong? Thank you in advance!
Answer by BinaryCaveman · Dec 22, 2010 at 04:19 AM
I found a solution to my problem.
Although it is probably not the best possible solution, I placed the scripts I was referring to in the Standard Assets
folder inside my project.
In this way, Unity compiles the scripts I was referring to before the script in which I wrote GetComponent<ScriptName>()
. (See the Unity documentation for more info)
Thanks, that works perfect! Iv'e been working a few hours trying to figure this out, and finally I found your answer!
Answer by Proclyon · Dec 21, 2010 at 11:19 PM
ScriptName is not a type that exists in your scope. Most of the time that just means you got the name wrong. But with the current information , that's all I can guess. Either that or you built it in some other project or library that isn't included yet and you need to import it with a using reference, the compiler friendly as it is, assuming you never make a typo.
This script is in C#, where the other script is in JavaScript. Does that make a difference?
Answer by NinjaEntertainment · Sep 22, 2017 at 07:52 PM
Hello. First that script u are disabling must exist. you need to change that ScriptName to real scriptname to your REAL script name instead of "ScriptName"
Your answer
![](https://koobas.hobune.stream/wayback/20220613092012im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Error with unify code? 1 Answer
Unexpected token ) when dealing with getcomponent 1 Answer
C# getComponent result error cast 3 Answers
Enable script on parent 2 Answers
I am getting a error CS0246: 1 Answer