Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Mixing C# and Javascript, how?

Discussion in 'Scripting' started by steddyman, May 18, 2009.

  1. steddyman

    steddyman

    Joined:
    Apr 10, 2009
    Posts:
    253
    I have a number of scripts in my Project that are C# and about an equal number that are Javascript. They are mainly the scripts I have found on the Wiki and the 2D tutorials and then modified to my needs.

    There seems to be a real issue having a C# access Javascript objects of vice versa. I found the following article in the help:

    http://unity3d.com/support/documentation/ScriptReference/index.Script_compilation_28Advanced29.html

    However, this is really only helpful if you are trying to write plugins or Editor Scripts.

    I don't want to have to start placing none-Editor components in Editor directories or moving some scripts based on the language used into Standard Assets, this is plainly stupid.

    What is the correct way to mix scripts which are both game scripts but in different languages without encountering the dreaded:

    CS0246: The type of namespace 'xxxxx' could not be found. Are you missing a using directive or an Assembly reference.

    Thanks
    Steddy
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    You need to put the "to be accessed" script in one of the folders thats compiled earlier.
    That does not only mean the editor folder, but also Plugins, Standard Assets and Pro Assets

    That way scripts of the other languages are able to work with it.

    The only alternative is not to access it directly at all and use SendMessage instead

    The least amount of trouble and headache is present if you just focus on one language instead of mixing 2+ languages
     
  3. steddyman

    steddyman

    Joined:
    Apr 10, 2009
    Posts:
    253
    Thanks

    That is a real pain because my scripts are interdependent.

    So, while I may have a C# component calling a Javascript component, the Javascript component may then rely upon another C# component.

    Is there no implications from dropping a script into Plugins when it isn't a plugin? I only have an iPhone Basic license so I am not sure if it even supports plugins.
     
  4. Lucas Meijer_old

    Lucas Meijer_old

    Joined:
    Apr 5, 2008
    Posts:
    436
    I think you should be fine if you put it in Plugins.
    However, this will stay very annoying, as from your c# script in Plugins, you cannot access the types defined in your javascript file outside of plugins.

    This indeed is not a great situation. I agree with dreamora that the path of least resistence is probably to convert your stuff to a single language.

    I'd personally recommend c# :)

    Bye, Lucas
     
  5. steddyman

    steddyman

    Joined:
    Apr 10, 2009
    Posts:
    253
    Thanks Lucas

    I do quite a bit of development with C# anyway for my ASP.NET development.

    I think Unity needs to resovle this complex mess. Otherwise it is impossible to generate a community where scripts cannot be easily shared between members.

    There isn't a lot of point to me taking scripts off the Wiki if I then have to rewrite them before I can use them.

    Steddy
     
    GreenAsJade likes this.
  6. amit-chai

    amit-chai

    Joined:
    Jul 2, 2012
    Posts:
    80