Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Can I use JS to access C# methods/classes without MonoBehaviour?

Discussion in 'Scripting' started by awesomedata, May 11, 2015.

  1. awesomedata

    awesomedata

    Joined:
    Oct 8, 2014
    Posts:
    1,419
    I have 2 scripts -- a C# script that does some math, and one JS/UnityScript that should utilize the C# methods. Only the UnityScript is attached to a game object. This is because the class I want to use (in the C# script) doesn't inherit from MonoBehaviour.

    For some reason, no matter how I set up the scripts or variables, I keep getting the "Unknown identifier: OSM" BCE0005 error. I've put the C# script into the "Standard Assets" folder so it will compile before the UnityScript files, but for some reason, the JS still can't see my OSM class and its methods!

    Here's my code (Note, the Update function is omitted for brevity):


    Javascript :
    Code (csharp):
    1.  
    2. function MapPosition() {
    3.   var osm = new OSM();
    4.   var tx : Vector2;
    5.   tx = OSM.WorldToTile(fixLon,fixLat);
    6.   }
    7.  
    C# :
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System;
    5. public class OSM {
    6.   public Vector2 WorldToTile(double lon, double lat, int zoom)
    7.   {
    8.   Vector2 p = new Vector2();
    9.   p.x = (float)((lon + 180.0) / 360.0 * (1 << zoom));
    10.   p.y = (float)((1.0 - Math.Log(Math.Tan(lat * Math.PI / 180.0) +
    11.   1.0 / Math.Cos(lat * Math.PI / 180.0)) / Math.PI) / 2.0 * (1 << zoom));
    12.  
    13.   return p;
    14.   }
    15.  
    16.   public Vector2 TileToWorld(double tile_x, double tile_y, int zoom)
    17.   {
    18.   Vector2 p = new Vector2();
    19.   double n = Math.PI - ((2.0 * Math.PI * tile_y) / Math.Pow(2.0, zoom));
    20.  
    21.   p.x = (float)((tile_x / Math.Pow(2.0, zoom) * 360.0) - 180.0);
    22.   p.y = (float)(180.0 / Math.PI * Math.Atan(Math.Sinh(n)));
    23.  
    24.   return p;
    25.   }
    26. }
    I'd like some code examples of how to do this with static C# methods and with an object instance to call the C# methods if possible. The way I'm doing this is apparently not right and I don't know why .

    I've been at this for months and cant find any documentation that states explicitly how to do this. All I get is "put your class scripts in the Standard Assets / Plugin folder" and it solves it for everyone except me it seems. There's got to be a problem with my code because I've got my C# and JS files separated and my C# is in Standard Assets while my JS is in the regular folder. D:

    Thank you in advance to anyone who can and cares to help me out with this!
     
    Last edited: May 11, 2015
  2. K0lipser

    K0lipser

    Joined:
    May 12, 2015
    Posts:
    8
  3. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    I think the first question we should ask is...why are you doing this?
     
    Kiwasi and dterbeest like this.
  4. K0lipser

    K0lipser

    Joined:
    May 12, 2015
    Posts:
    8
    For example, to access variables of the First Person Controller Script from a JS-Script. Needed that too in the past, but then, I wrote my own FPS-Script. ;)

    Example: Fire event, if player touches ground. Or player is in the air. Or anything.
     
  5. awesomedata

    awesomedata

    Joined:
    Oct 8, 2014
    Posts:
    1,419
    Ever had an extension/asset or other 3rd party code you'd prefer to access with one language but it's written in another?

    I'd like to be able to access that code/class/method whenever I buy a complicated asset written in a language other than my chosen one or simply when one language doesn't offer the functionality of the other language (i.e. C# constructors) and it's simply not possible to easily convert some assets from C# to JS or vice versa without breaking them. I'd rather write a simple library that interfaces between the two (if necessary) or access the functionality directly through the other language if possible.

    Sure, it's "bad coding practice" to mix languages -- but I still want to know how to do this. Anyone who knows how to access methods/classes in C# through UnityScript?

    The closest example I could find was this:

    http://www.41post.com/1935/programming/unity3d-js-cs-or-cs-js-access

    The problem with that project download is it only lets one access variables from C# and not proper methods with arguments (which I can't seem to do without an error!), nor can I access C# classes/methods from JS/UnityScript that were not derived from MonoBehaviour. Anyone have any idea why?



    I'm highly aware of the compile time differences, but even taking that into account, it does not work. My C# scripts, as mentioned before, are in the Standard Assets folder (I even tried a Plugin folder too!) while my JS files are in regular (non-special) folders, but still no avail.

    I'm using the final release of unity 4.6 if it helps. I'm not sure if I'm perhaps encountering a bug in my version?