Accessing C# variable from JavaScript?

I know in Java all I need to do is add the static keyword in front of my var definition. How do I access my variable in C#?

using UnityEngine;
using System.Collections;

public class CameraSwitch : MonoBehaviour
{
	public Camera FirstPersonCamera;
	public Camera ThirdPersonCamera;
	private bool Activated = false;
	private float elapsedTime = 0F;
	public enum DefaultCamera { First = 1, Third = 3 }
	public DefaultCamera View = DefaultCamera.Third;

I want FirstPersonCamera and ThirdPersonCamera available in my javascript, but I get this error:

When I try to access my variable:

var MainCamera = CameraSwitch.ThirdPersonCamera.camera;

Thank you.

If you want to do it static like you mentioned, you could say:

public static Camera ThirdPersonCamera;

… then access the camera like you are doing now.

However you could also keep the class definition as it is and do:

var MainCamera = GameObject.Find(“InventoryBackground”).GetComponent().ThirdPersonCamera.camera;

http://unity3d.com/support/documentation/ScriptReference/index.Script_compilation_28Advanced29.html

Making a variable static is not how you access them from other scripts. http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

Just put ur csharp script in Standard assets folder then after u can call csharp variable in javascript.

Accessing a component from a C# script should work exactly the same way as accessing a component from Javascript unless the javascript is compiled BEFORE the C# script (like if it’s in the Prefabs or Editor folder).

Manny pretty much has it right…

the code you posted isn’t quite right for accessing the component though. it should be something like this:

var camaraObject : GameObject;  // declaring the camera object...you can drag the camera object on to the script in the inspector this way
var cameraScript : CameraSwitch;  // this is your C# script that's attached to the cameraObject game component


public Start(){  // for performance reasons, it's always good to get your components in the Awake or Start function

    cameraScript = cameraObject.GetComponent(CameraSwitch);

    // then you can access the vars in the camera script class similarly to what you had:

var MainCamera = cameraScript.ThirdPersonCamera.camera;

}

you can also declare your ThirdPersonCamera variable as static, but then it won’t be accessible through the inspector. One way around this that I use sometimes is to have two vars, a public var that’s accessible through the inspector and then a static var that’s accessible everywhere. then in my Start() function I assign the public var to the static var.

One caveat about using static vars is that you always have to access them by referencing the script (even inside the same script).

So if you made the camera a static var:

 public static ThirdPersonCamera;

you would always have to access it like this:

 CameraSwitch.ThirdPersonCamera = whatever

even within the CameraSwitch script.

Sorry to necro post this one, but we work with JS and C# in the same project and I have an issue.
I need to pass a boolean method from C# toJS.

C#:

        public bool ReadyToPlay()
        {
            return isReady;
        }

JS:

script_clientSceneFlowTrigger.SendMessage("ReadyToPlay") ? completed[0] : !completed[0];

My goal is to pass a Bool, and if the bool is true I continue to the next level.

My error is:
Expressions in statements must only be executed for their side-effects.
Which means this code is not doing anything…

I can’t use the SendMessage as a Boolean and put it in if statement.

NOTE: Before anybody tells me to use one language… I know it’s the best, but currently we go with two.

UnityScript has been deprecated for a long while now, so there’s probably not a lot of people around that remembers the intricacies of interop anymore.

1 Like

Yes it’s a bit difficult… You probably right