Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by TomatoOrgyLT · Aug 13, 2012 at 01:15 AM · c#javascriptdisableunknown identifier

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kryptos · Aug 14, 2012 at 10:48 AM 0
Share

Best advice: don't mix JS and C# scripts in the same project.

avatar image aldonaletto · Aug 14, 2012 at 12:59 PM 0
Share

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!

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.
Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kryptos · Aug 13, 2012 at 08:48 AM 0
Share

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.

avatar image Meltdown · Aug 13, 2012 at 09:55 AM 0
Share

Aldo is correct, and this is my biggest ballache issue with Unity.

avatar image TomatoOrgyLT · Aug 13, 2012 at 08:25 PM 0
Share

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)

avatar image aldonaletto · Aug 14, 2012 at 11:16 AM 1
Share

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?

avatar image aldonaletto · Aug 14, 2012 at 02:08 PM 1
Share

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");
   ...

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How to Calling function in JS from C# 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges