How do I use JS to access C# methods/classes?

I have 2 scripts – a C# script that does some math, and one JS/UnityScript that should utilize its methods. Only the UnityScript is attached to a game object.

For some reason, no matter how I set up the scripts or variables, I keep getting the “Unknown identifier: OSM” BCE0005 error. I’ve even went so far as to put the C# script into a Plugins folder so it will compile earlier, but for some reason, the JS still can’t see the OSM class and its methods!

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

Javascript :

function MapPosition() {
  var tx : Vector2;
  tx = OSM.WorldToTile(fixLon,fixLat);

C# :

using UnityEngine;
using System.Collections;
using System;

public class OSM {

	public Vector2 WorldToTile(double lon, double lat, int zoom)
		Vector2 p = new Vector2();
		p.x = (float)((lon + 180.0) / 360.0 * (1 << zoom));
		p.y = (float)((1.0 - Math.Log(Math.Tan(lat * Math.PI / 180.0) + 
		                              1.0 / Math.Cos(lat * Math.PI / 180.0)) / Math.PI) / 2.0 * (1 << zoom));
		return p;
	public Vector2 TileToWorld(double tile_x, double tile_y, int zoom) 
		Vector2 p = new Vector2();
		double n = Math.PI - ((2.0 * Math.PI * tile_y) / Math.Pow(2.0, zoom));
		p.x = (float)((tile_x / Math.Pow(2.0, zoom) * 360.0) - 180.0);
		p.y = (float)(180.0 / Math.PI * Math.Atan(Math.Sinh(n)));
		return p;

You are trying to use the OSM class without an object instantiation. Please try

  1. new OSM object before calling methods


  1. make the methods static so that you don’t need an object instance to access them.

It required being put into a new project for some reason. After that, it worked fine. I’m assuming it was a fluke with my asset database or something. :confused:

Sooo strange… :frowning: