0

I want to call C# function from a UnityScript GameMaster.js like this.

GameMaster.js

#pragma strict

function Start () {
  print(Mahjong.Foo());
}

Mahjong.cs

using System.Collections;

public class Mahjong {
  string Foo () {
    return "foo";
  }
}

When I run the program, I get BCE0005: Unknown identifier: 'Mahjong'.. According to information from here, if script files are in Assets directory, I don't need to import file.

But it doesn't works in my case. what am I missing? How can I call #C's function from UnityScript?

0

1 Answer 1

2

The C# script has to be compiled before the JS script for this to work. To achieve this you have to move the C# script into one of the following directories: Plugins, Standard Assets or Pro Standard Assets.

It also works the other way around - if you want to call JS from C#, move the JS into one of the folders.

It does not work both ways at once however.

Sign up to request clarification or add additional context in comments.

1 Comment

You can also use SendMessage to invoke a C# method from Javascript (or vice-versa), but your are limited to passing one parameter, and you can't get the return value. So pretty limited, but sometimes useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.