- Home /
Disable script does not work
Ok, so I want to disable my C# script with my javascript code
function Start() {
GetComponent(movegui).enabled = false;
}
the code should disable my C# code, the thing is it shows me an error
Assets/Standard Assets/hover Script.js(14,14): BCE0005: Unknown identifier: 'movegui'.
The name of my C# code is movegui so why it still shows me this error and how to fix it? Thank you for help.
Best advice: don't mix JS and C# scripts in the same project.
Yes, mixing languages in Unity is still a pain in the ass. $$anonymous$$y suggestion to the Unity $$anonymous$$m: make the JS compiler a kind of pre-processor that simply converts JS to C#, and compile everything as C#. This would allow full compatibility between both languages, and even the use of inline C#, a very handy feature. Better yet, the same pre-processor could be used in C# files as well, allowing inline JS - this would be heaven for everybody!
Answer by aldonaletto · Aug 13, 2012 at 01:50 AM
C# and JS can't see each other during compilation because the compilers are different. You must place the C# script in a folder that will be compiled before the JS script - take a look at Script Compilation (Advanced) in the docs to know which folders are compiled first.
But in this specific case, you can work around this by simply getting the script with the string version of GetComponent:
function Start(){ (GetComponent("movegui") as MonoBehaviour).enabled = false; }The string GetComponent version searches for the target script by name at runtime, thus the compilation order doesn't matter.
C# and JS can't see each other during compilation because the compilers are different.
Not sure that is the reason. I'm guessing this is because the C# assembly references the JS assembly, while the JS assembly does not reference the C#. Assemblies cannot reference each other due to a .Net policy.
Aldo is correct, and this is my biggest ballache issue with Unity.
Thanks it worked, but ummmm... I get another error, and it says
NullReferenceException: Object reference not set to an instance of an object hover Script.Update () (at Assets/Gi$$anonymous$$es medis/$$anonymous$$odai/hover Script.js:52)
Here is my full script
var levelToLoad : String; var soundhover : AudioClip; var beep : AudioClip; var QuitButton : boolean = false;
var ShowGui : GUITexture; var ShowGuiTwo : GUITexture; var ShowGuiThree : GUITexture;
function Start() {
GetComponent(movegui).enabled = false; // The cause of the problem
ShowGui.enabled = false; ShowGuiTwo.enabled = true; ShowGuiThree.enabled = false; }
function On$$anonymous$$ouseEnter(){ audio.PlayOneShot(soundhover); renderer.material.color = Color.yellow; }
function On$$anonymous$$ouseExit() { renderer.material.color = Color.white; }
function On$$anonymous$$ouseUp(){ audio.PlayOneShot(beep); yield new WaitForSeconds(0.35);
GetComponent(movegui).enabled = true; // The cause of the problem
ShowGui.enabled = true; ShowGuiTwo.enabled = false; ShowGuiThree.enabled = true;
if(QuitButton){ Application.Quit(); } else{ Application.LoadLevel(levelToLoad); } }
function Update() {
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.Escape)){
GetComponent(movegui).enabled = false; // The cause of the problem
ShowGui.enabled = false;
ShowGuiTwo.enabled = true;
ShowGuiThree.enabled = false;
}
}
@script RequireComponent(AudioSource)
The error is in line 17 - which line is it? If it's moveguiScript.enabled = false;, then the script movegui.cs isn't being found by GetComponent - are movegui.cs and hoverScript.js attached to the same object?
Bingo! GetComponent alone searches only in the script owner object - you should use someRef.GetComponent in order to get a component from the object referenced by someRef:
var moveguiOwner: GameObject; // drag here the object to which movegui is attached
var moveguiScript: $$anonymous$$onoBehaviour;
function Start(){
moveguiScript = moveguiOwner.GetComponent("movegui");
...
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Can't use script name as variable type? 2 Answers
Distribute terrain in zones 3 Answers
Disable JS from C# 1 Answer