Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[partially solved] accessing JS from C# and JS (Script Execution Order)

Discussion in 'Scripting' started by Shushustorm, Apr 25, 2015.

  1. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    Hello everyone!
    I have read that when trying to access JS from C# or vice versa, I have to make sure that one of the scripts is compiled before the other one is.
    Most of the time, it is suggested to put one of the scripts into the Plugin or Standard Assets folder so that it will be compiled first.
    As I would like to keep the scripts where they are, I have looked for a way to do this differently and found the Script Execution Order in Edit -> Project Settings.
    Will accessing JS from C# work, if I put the specific JS and C# scripts in there and arrange the JS before the C# script?
    This is what I did:
    screenshot.PNG
    (scenes_script.js, turning_script.js, online_script.js, online_script2.cs - trying to access scenes_script.js from online_script2.cs)
    Because right now, I am not able to access the JS, but I guess I might be doing something wrong in the C# code, because moving the JS to Plugins won’t get rid of this error either:

    […]/online_script2.cs(132,1): error CS0246: The type or namespace name `scenes_script' could not be found. Are you missing a using directive or an assembly reference?

    The C# code I use:

    Code (CSharp):
    1. scenes_script scenes_script_ = scenes_script_obj.GetComponent<scenes_script>;
    Does anyone have an idea what’s wrong?

    Thank you very much,
    Greetings,
    Shu
     
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    not sure if this will fix it, but the correct way to use GetComponent in C# is:

    Code (csharp):
    1.  
    2.  
    3. scenes_script scenes_script_ = scenes_script_obj.GetComponent<scenes_script>();
    4.  
    5.  
    Also, your class names and variable names are very confusing. You might want to use something more generally accepted like ScenesScript as a class name.
     
  3. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    Thank you very much for your answer, Fluzing!
    Unfortunately, this didn't get rid of the error.

    By "accepted", do you mean that Unity might have a problem with my nomenclature?
    Because so far, I haven't had any problem with class names except for that one time when I wanted to use "0000_" as a prefix for a script.

    EDIT:
    Do I have to start a script name with a capital letter to do this?
    In the script component, "scenes_script" is being displayed as "Scenes_script (Script)".
    Accessing one of the JS from another JS does work, though.
     
    Last edited: Apr 25, 2015
  4. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    No, it should work fine I think, but it is better to use names in a format that others use as well. That way your code is easier to read and it helps to prevent you making mistakes in the long run. I had to do bit of puzzling right now to get through this bit of code.
     
  5. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Are you using an asset that uses JS by the way? The reason I ditched JS is this type of problem with scripts. Writing everything in C# has made my life a whole lot easier.
     
  6. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    If you want to use UnityScript code from C#, you have to make sure that the UnityScript parts you want to use in C# are compiled before that C# code. And as you wrote, you can only do that by placing the scripts in special folders, like Plugins or Standard Assets. There is no way around it if you want to mix programming languages.
    The script execution order has nothing to do with it. This is something completely different.
     
  7. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    Okay, I guess I should rewrite code when asking questions about it, then.
    Because to me it is much easier to read when there is a little space between the words.

    I'm not completely positive, but after looking through my Assets (from the AssetStore), all of them seem to use C# only.

    Alright, thank you very much for your answer, Dantus!
    As already stated, moving the script to Plugins made the same error appear. Anyway, I will try this once more and maybe restart Unity just to make sure.
     
  8. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    Alright, this time I tried moving the script to Standard Assets rather than Plugins.
    For whatever reason, this worked fine.
    So the problem is solved. a new problem appeared. (see below "EDIT")
    Still, I think it's a really awkward approach to access scripts of the other language and it's really confusing if scripts aren't located where their location makes sense (e.g. "ControlsLevel01" in the level01 folder).
    I guess I will have to create some aliases, then.

    In future projects, I might also use C# only. I just really like coroutines in JS better and some value assigning in C# is just more effort.

    Again, thank you Fluzing and Dantus for your help!

    EDIT:
    After moving the JS to the Standard Assets folder, accessing it with C# does work, but now I get the following warning:
    The referenced script on this Behaviour is missing!

    and the JS to JS reference stopped working, even though in the Unity editor, the script is clearly not missing and the object the script is attached to, is assigned, too.

    EDIT2:
    Well, I am translating the whole project to C# now.
    The problems that keep appearing when trying to combine the languages are just hilarious.
    Should have started with C# after all..
     
    Last edited: Apr 25, 2015