- Home /
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? http://unity3d.com/support/documentation/ScriptReference/index.Member_Variables_26_Global_Variables.html
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?
Scripts:
ViewsBar.js
**HAVE TRIED ALL THE DIFFERENT DECLARATIONS**
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); }
smoothfollow.js
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);
}
I hit the link button below on the answer, I have no idea what that does nor am I able to do anything with the page where I hit the link, I BAC$$anonymous$$ed here to be able to type this note. How do I reverse the Link button mistake?
This is extremely important. Thank you. You need to write blog posts or something.
Answer by Mike 3 · Jun 27, 2010 at 08:04 PM
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
Please be clear as I am now very confused. I use Javascript(EC$$anonymous$$A... etc), Live Script from Netscape days) (browser clientside coding and ServerSide). Is Unity Javascript your compiler proprietary version not the EC$$anonymous$$A javascript? If so, where the keywords reference for it?. ...your documentation uses the name javascript, that leads me to believe it is javascript, plus all the extensions are js? Please clear up this issue for me, and do not forget to let me know where the ref is... not the tutorials, not the ref called Scripting on the Unity web site... no ref there that I saw.
It's EC$$anonymous$$A-ish, but it's a proprietary Unity only language. The best documentation on the language you'll get is probably in the wiki, here: http://www.unifycommunity.com/wiki/index.php?title=Program$$anonymous$$g
If only EC$$anonymous$$A-ish then not the same, thus problems. geees... I was afraid you would say that... now I have to dig into a new reference set to see if what I attempt to code using my (true)javascript understanding will fly in this version. Before I start digging, can you tell me if classes in Unityjavascript are attained the same way as in (true)javascript by nesting functions/prototyping? ... and are closures also a way of life in Unityjavascript? I am going to start a thread for 'DIFFERENCES between (true)javascript and Unityjavascript'.
ok... first 2, I understand now. (3)compiles earlier? I don't buy that unless the compiler is an archaic 1 pass compiler. (4)$$anonymous$$oving 'it' out, you mean the ViewsBar.js? I have moved it under several folders in the Standard Assets hierarchy. (5)do it the other way around... and what you state is what I am doing already? I even have moved the toobar code in the smoothfollow.js and got it to see the object, but now the buttons work for 2 clicks and then become dead. (6) I am not sure what your last comment about game specific versus feature means?
the third point is because Unity has multiple passes so that javascript and c# scripts from later passes can use common scripts in either language which are in specific folders. 4 i meant SmoothFollow, into some other folder than standard assets 5 and 6 are the same point, you wouldn't want any toolbar code in smoothfollow as it breaks reusability - if you needed to have 2 objects following things, it'd break things.
Your answer
Follow this Question
Related Questions
Is there any way to edit variables inside other scripts without declaring them as static? 1 Answer
Prefab's Script affecting all the Prefab 1 Answer
Variable Not Changing. 0 Answers
Unknown identifier, OnCollisionEnter Error 1 Answer
An instance of type X is required to access non static member Y [javascript] 4 Answers