- Home /
Calling c# function from a JS
Hi I have a script (written in c#) that I need to use from my unity script system. I have looked around at how to invoke a function inside the c# script but cannot find anything that helps me.
Before you ask, the c# files are located in the standard assets and get compiled before the java script files.
The error I get when trying to invoke the function is as follows: "It's not possible to invoke an expression of type 'error'. (BCE0077)
The c# function has a void return type. See the complete code below.
//GameMaster.js
enum GameMode{MENU, PLAYING};
var playerSpawn : GameObject;
var playerPrefab : GameObject;
var player : Player;
var toggleType : String;
var gameTimeElapsed : double;
var maxGameTime : double;
var gameMode : GameMode;
var stringToEdit : String;
var gameFont : GameFont; //c# script
var enemies = Array();
function Awake()
{
gameFont = gameObject.GetComponent(GameFont); //get the c#script
}
function Start()
{
gameFont.AddText("Hello", 32, 40); //error see post
//SetupMenu();
//SetupGame();
}
//GameFont.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GameFont : MonoBehaviour
{
public TextAsset fontXML;
public SpriteManager spriteManager;
BitmapFont bmf;
void Start ()
{
spriteManager = GameObject.Find("SpriteManager").GetComponent<SpriteManager>();
bmf = new BitmapFont(fontXML.text, 0, 0);
}
void AddText(string text, int x, int y)
{
AddText(bmf, text, x, y);
}
void AddText(BitmapFont bmf, string text, int x, int y)
{
List<BitmapFont.Sprite> sprites = bmf.GetSprites(text, 0, 0);
foreach(BitmapFont.Sprite s in sprites)
{
GameObject go = new GameObject("Glyph \"" + s.c + "\"");
go.transform.position = new Vector3(s.x + x + s.texCoords.width/2, Screen.height - y - s.y + s.texCoords.height/2, 0.0f);
spriteManager.AddSprite(go, s.texCoords.width, s.texCoords.height, (int)s.texCoords.x, (int)s.texCoords.y + (int)s.texCoords.height, (int)s.texCoords.width, (int)s.texCoords.height, false);
}
}
}
I am using the package created by this guy: http://forum.unity3d.com/threads/87782-Pixel-Perfect-Bitmap-Font-in-Unity
gameFont is != null in Awake? The JS isn't in PlugIns or Standard Assets? Oh and compile or run time error?
gameFont is not null, the JS's are in "$$anonymous$$yAssets/Scripts" the c# scripts are in "StandardAssets/Scripts"
Edit: Compile Time error - Scripts won't compile - oh and its the only error so not caused by another error
Since you mention having the scripts in the standard assets folder, did you mean the plugin folder? I once had a support reply telling me to place cs scripts there to have them compiled first.
As far as i'm aware both StandardAssests and the Plugin folder are compiled before user created folders such as $$anonymous$$yAssets
But no I did mean the StandardAssestFolder
Answer by whydoidoit · Mar 25, 2013 at 06:05 PM
That AddText call doesn't appear to be public...
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
Play music trough scenes 0 Answers
Fonts not displaying properly when loaded through script 0 Answers
I can do this in JS or not ??? 2 Answers