- Home /
Access a JavaScript variable from a C# script?
I just started integrating the 2D toolkit into a little demo I've been working on. I've been doing everything in JavaScript but the 2DToolkit stuff all seems to be in C#. The scenario is I have a Hero(JS) who detects collision with coins, and I need to increment the score counter (C#). I've tried somethings and haven't been able to make it work. Here's my HUD script:
using UnityEngine;
using System.Collections;
public class TextMeshExample : MonoBehaviour {
tk2dTextMesh textMesh;
int score = 0;
// Use this for initialization
void Start () {
textMesh = GetComponent<tk2dTextMesh>();
}
// Update is called once per frame
void Update () {
//replace with something like textMesh.text = "SCORE: " HeroController.coinCount
if (Input.GetKey(KeyCode.Q))
{
score++;
textMesh.text = "SCORE: " + score.ToString();
// This is important, your changes will not be updated until you call Commit()
// This is so you can change multiple parameters without reconstructing
// the mesh repeatedly
textMesh.Commit();
}
}
}
and here's part of my hero script:
function OnCollisionEnter(collision : Collision) {
if(collision.gameObject.name == "WaterBarrier") // If A collides with B
{
if (collision.relativeVelocity.magnitude > outOfWaterThreshold/100)
{
//Physics.gravity = Vector3(0, -1.5, 0);
//transform.position = Vector3(-0.6948604, .5, 3.701174);
print("We just hit the water level... Hard!!!");
//Physics.gravity = Vector3(0, 3, 0);
//transform.position = Vector3(-0.6948604, -.5, 3.701174);
}
}
//If we hit a coin increment the count and destroy it
if(collision.gameObject.name == "Coin" || collision.gameObject.name == "SilverCoin")
{
transform.position = Vector3(transform.position.x, transform.position.y, 3);
Destroy(collision.gameObject);
if(collision.gameObject.name == "Coin")
{
coinCount = coinCount+1;
}
else if(collision.gameObject.name == "SilverCoin")
{
coinCount = coinCount+2;
}
}
//If we hit a hazard increment the count and restart the level
if(collision.gameObject.name == "Hazard")
{
Application.LoadLevel ("ProofOfConcept");
hazardCount = hazardCount+1;
}
}
I'm trying my best, so if somebody could give me some insight I'd really appreciate it! Thanks so much!
Answer by imnickb · Mar 25, 2013 at 07:30 PM
A little more info on this in case anyone's in the same boat as me; looking to use 2DToolKit with JavaScript. The 2DToolKit itself is scripted using C# so there does need to be some communication between JavaScript and C# if you wish to script in JavaScript. You need to adjust where some things are located and in what order things are compiled to get this to work, but the good news is that Unikron has set up the 2DToolKit to take care of all that for you. Just go to the 2DToolKit menu and select "Setup for JavaScript." After that you can do your 2DToolKit scripting in JavaScript, you just need to adapt the syntax in some of the tutorials since they're all written in C#. I hope this might help someone in the future.
http://unikronsoftware.com/2dtoolkit/forum/index.php/topic,1381.0.html
Answer by nsxdavid · Mar 25, 2013 at 05:11 AM
JavaScript and C# have difficulty talking to each other because of the order of compilation.
Opinion: Use C# please. UnityScript is the devil.
More detail: http://docs.unity3d.com/Documentation/ScriptReference/index.Script_compilation_28Advanced29.html