Search Unity

Disable javascript component with csharp

Discussion in 'Scripting' started by ki_ha1984, Mar 2, 2015.

  1. ki_ha1984

    ki_ha1984

    Joined:
    Aug 24, 2014
    Posts:
    111
    Hi,

    I need to disable the CharacterMotor(A) component, from a csharp(B) component, i want to disable the A component from the code of the B. and i cannot.
    I usually use this code, and i attached the component from the editor.

    Code (CSharp):
    1. public Component walkCom;
    2. if (Input.GetKeyDown(KeyCode.Escape)) {
    3.        walkCom.enabled != walkCom.enabled;
    4. }
    The problem is that the csharp compiler cannot find the javascript component.

    Any idea ?
     
  2. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    Advice...

    If you're a hobbyist, pick 1 of the 2 languages and stick with it.
    If you want to do this professionally, use C#.

    You can put your javascript files in standard assets or plug-ins (one of those, I think) and then you will be able to access js from your C#. However, it's impossible to go both ways. Either you access C# from JS or JS from C#. It's unfortunate for beginners that it has to be one way or the other, but it really is better to just learn one of the two languages (preferably C#) and use just that.
     
  3. lineupthesky

    lineupthesky

    Joined:
    Jan 31, 2015
    Posts:
    92
    As mentioned above, you can use Phase References over here to decide which folder you should put the C# or JS script. Also as Tomnnn said, it's better to go with only one language. But in some cases, eventhough you picked for instance C#, and need to deal with some JS scripts that are built in with Unity, for example some of the Image Effect scripts, you can use Send Message.

    Just simply write a JS script like this:

    Code (JavaScript):
    1. var script : ScriptName;
    2.  
    3. function Disable()
    4. {
    5. script.enabled = false;
    6. }
    And then, in C# script, reach to the object which holds the JS, and call this:

    Code (CSharp):
    1. holderObject.SendMessage("Disable");
    this will trigger the Disable function in JS eventhough you sent it from C#, and it will disable the attached JS script.
    Note that the function name is important, it's better not to match with another function in that object, also note that SendMessage is an expensive process.
     
  4. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Component doesnt have the enabled property afaik. Use MonoBehaviour instead and your original code should work, without having to mess with script compilation order.
     
    lordofduct likes this.
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    I'd like to point out that technically this is wrong.

    I say technically... because it's not a matter of just the language to language, but of what gets compiled when.

    Example, you can put both js and cs in the plugins folder, and js and cs in the regular assets folder can access both, but can't access each other, and the plugins code can't access the code in the regular assets folder.

    Check here:
    http://docs.unity3d.com/Manual/ScriptCompileOrderFolders.html

    Anything in a higher phase can access a lower phase (except that things in phase 3 should NOT be accessing phase 2, becuase runtime scripts should not access editor scripts).

    And then within the same phase, you have the inter-language issue. JS gets compiled before CS, so you can only access in the higher phase of this (CS to JS).

    When it comes to plugins and the sort, they should intercommunicate anyways. They're supposed to be self contained plugins for your use from your scripts. As long as a single plugin uses the same language through out, there won't be an issue (this also usually isn't your concern, because you usually get plugins from a 3rd party, they hammer out that stuff for you).

    When within YOUR code, stick to a single language.





    As for OP, thing is you are accessing JS from CS within the same phase. For that, ThermalFusion is spot on:

    Enabled is defined in the 'Behaviour' class. Not all Components are considered Behaviours, and don't need the enable/disable feature.

    Note also that 'Collider' DOES have the enable property, but inherits from 'Component' and not 'Behaviour'. It independently defines the same property. This is really annoying when looking for a common interface to enable and disable components with.

    For that I have some extension methods of my own:

    Code (csharp):
    1.  
    2.         public static bool IsEnabled(this Component comp)
    3.         {
    4.             if (comp == null) return false;
    5.             if (comp is Behaviour) return (comp as Behaviour).enabled;
    6.             if (comp is Collider) return (comp as Collider).enabled;
    7.             return true;
    8.         }
    9.  
    10.         public static void SetEnabled(this Component comp, bool enabled)
    11.         {
    12.             if (comp == null) return;
    13.             else if (comp is Behaviour) (comp as Behaviour).enabled = enabled;
    14.             else if (comp is Collider) (comp as Collider).enabled = enabled;
    15.         }
    16.  
    found:
    https://code.google.com/p/spacepupp...e/trunk/SpacepuppyBase/Utils/ComponentUtil.cs
     
  6. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    @lordofduct that does sound more like the thing I was trying to remember. Compile time stuff.

    Still, you'll be more valuable (unless you're a hobbyist) if you stick with C# and convert any js code you need to C#. Performance may be negligible, but try explaining that to the non-technical boss man signing the checks.
     
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    Totally agree.