Global variables - static keyword ? UnityScript? javascript?

Update: Now that I understand more, I would like to make it clear to others who might struggle with this issue.

  • 'The Unity docs, forums, and communtiy refer to the scripting language used in conjunction with the Unity Engine as 'javascript'
  • The scritping language in Unity should be named and referred to as 'UnityScript'
  • Some of the answers I got or seen on this issue are all over the map, stating: it is JSCript like, C# based, Boo based, ... geez at this point who cares, no-one seems to know.
  • What is obvious is that it is not 'javascript'(ECMA's web browser's implementation) nor 'JScript'(Microsoft's javascript implementation). [Note: no need to go back to the Netscape level, ECMA is base enough.]

THUS, CRITICAL: Treat this 'UnityScript' as its own implementation(Unity's). Stay aware that what you know in the world reknown great standard 'javascript' implementation may not apply in 'UnityScript' Example: the word 'static' is a reserved word in 'javascript', 'JScript', Java, etc. but is not implemented in 'javascript' nor JScript' but IS IMPLEMENTED in 'UnityScript'.

I am willing to bet that many programming practices we are used to in 'javascript' are not going to work in 'UnityScript' while others which are not available in 'javascript' are possible in 'UnityScript'. To name 2 of the more obvious and important ones: prototyping versus class based inheritance, and closures. ('UnityScript' appears to be class based yet may allow prototyping... don't know, have not had a chance to verify that)... and I believe closures are not available in 'UnityScript' ...someone please clarify and save me the trouble of having to test it... I will update this Update to keep it all in one place. t.y.

None of the answers I have gotten or ran into on this issue have been completely clear. They all left me with gaps and questions. Thus I have not upvoted any. The link Mike provides in the answer below is being rapidly updated, and offers the most complete reference I have seen in one place: Unityjavascript. I really wish the name 'UnityScript' were used instead of 'javascript' in the Unity context. (...even if the name 'javascript' serves as a marketing lure...)

~~~~~~ ORIGINAL ENTRY... ~~~~~~ Spun on this too long... something does not add up. My ViewsBar.js creates the toolbar, buttons appear on screen and trigger/change shade on mouse click. All good.

Note: I should mention that if one sweeps the mouse pointer off the button before releasing the mouse button, the button still triggers - that is inconsistent with the behavior on other Windows buttons. It gives no chance to cancel the action if the wrong button was clicked.

I have never seen a javascript 'static' keyword. - mentioned at this URL in the Script Reference?

Is it a new version? Is it one of the hidden secrets of javascript? Is there a UnityScript? ...but everything has .js extensions in the Reference.

ERROR: Assets/Standard Assets/Camera Scripts/SmoothFollow.js(31,15): BCE0005: Unknown identifier: 'ViewsBar'.

How do I read the status of the buttons in the toolbar from the smoothfollow script so I can adjust the camera location accordingly?




var toolbarInt = 0;           // should be global var
//toolbarInt = 0;             // should be global var if line is in function(not here)
//var toolbarInt;             // should be global var
//static var toolbarInt = 0;  // global var ?? never heard of static keyword in javascript

var toolbarStrings : String[] = ["Top", "Driver", "Observer"];

function OnGUI () {
  toolbarStrings = ["View: Top", "Driver", "Observer"];
  toolbarInt = GUI.Toolbar (Rect (25, 25, 200, 30), toolbarInt, toolbarStrings);


var target : Transform;

distance = 1.0;
var height = .8;
var heightDamping = 2.0;
//var heightDamping = 0;
var rotationDamping = 3.0;
//var rotationDamping = 3.5;

// Place the script in the Camera-Control group in the component menu
@script AddComponentMenu("Camera-Control/Smooth Follow")

print(ViewsBar.toolbarInt); **// have tried this outside and inside the LateUpdate function**

function LateUpdate () {
   if (!target) return;
      wantedRotationAngle = target.eulerAngles.y;
      wantedHeight = target.position.y + height;
      currentRotationAngle = transform.eulerAngles.y;
      currentHeight = transform.position.y;
      currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
      currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
      currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);

      transform.position = target.position;
      transform.position -= currentRotation * Vector3.forward * distance;
      transform.position.y = currentHeight;
      transform.LookAt (target);

Unity Javascript (a.k.a. UnityScript) is a .NET based language, so the static keyword works with it.

For your specific problem, static var toolbarInt = 0; would work, but it sounds like it can't find your script.

Smoothfollow is probably the one from standard assets? If that's the case, it can't find your other script because standard assets is compiled earlier

You could try moving it out of the folder somewhere else

Alternatively - do it the other way round. Have a function on your smoothfollow script which lets you set the current behaviour (from your other script)

That means that you're not adding game specific code into the follow script, just adding more features