Search Unity

Accessing C# variable from JavaScript?

Discussion in 'Scripting' started by Senkusha, Dec 29, 2011.

  1. Senkusha

    Senkusha

    Joined:
    Mar 28, 2011
    Posts:
    98
    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#?

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class CameraSwitch : MonoBehaviour
    6. {
    7.     public Camera FirstPersonCamera;
    8.     public Camera ThirdPersonCamera;
    9.     private bool Activated = false;
    10.     private float elapsedTime = 0F;
    11.     public enum DefaultCamera { First = 1, Third = 3 }
    12.     public DefaultCamera View = DefaultCamera.Third;
    13.  
    I want FirstPersonCamera and ThirdPersonCamera available in my javascript, but I get this error:
    When I try to access my variable:
    Code (csharp):
    1.  
    2. var MainCamera = CameraSwitch.ThirdPersonCamera.camera;
    3.  
    Thank you.
     
    Last edited: Dec 29, 2011
  2. Manny Calavera

    Manny Calavera

    Joined:
    Oct 19, 2011
    Posts:
    205
    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<CameraSwitch>().ThirdPersonCamera.camera;
     
  3. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
  4. UnityCoder

    UnityCoder

    Joined:
    Dec 8, 2011
    Posts:
    534
    Just put ur csharp script in Standard assets folder then after u can call csharp variable in javascript.
     
  5. Timbecile76

    Timbecile76

    Joined:
    Jul 8, 2009
    Posts:
    248
    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:
    Code (csharp):
    1.  
    2. var camaraObject : GameObject;  // declaring the camera object...you can drag the camera object on to the script in the inspector this way
    3. var cameraScript : CameraSwitch;  // this is your C# script that's attached to the cameraObject game component
    4.  
    5.  
    6. public Start(){  // for performance reasons, it's always good to get your components in the Awake or Start function
    7.  
    8.     cameraScript = cameraObject.GetComponent(CameraSwitch);
    9.  
    10.     // then you can access the vars in the camera script class similarly to what you had:
    11.  
    12. var MainCamera = cameraScript.ThirdPersonCamera.camera;
    13.  
    14. }
    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:

    Code (csharp):
    1.  public static ThirdPersonCamera;
    you would always have to access it like this:

    Code (csharp):
    1.  CameraSwitch.ThirdPersonCamera = whatever
    even within the CameraSwitch script.
     
  6. Eco-Editor

    Eco-Editor

    Joined:
    Jun 29, 2016
    Posts:
    71
    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#:
    Code (CSharp):
    1.         public bool ReadyToPlay()
    2.         {
    3.             return isReady;
    4.         }
    JS:

    Code (CSharp):
    1. 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.
     
    Last edited: Jun 18, 2019
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    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.
     
    Joe-Censored likes this.
  8. Eco-Editor

    Eco-Editor

    Joined:
    Jun 29, 2016
    Posts:
    71
    Yes it's a bit difficult.... You probably right