- Home /
unity javascript indirection
What is the syntax for creating a function in javascript which can modify the value of a given parameter ?
For example, the unity function Vector3.Smoothdamp modifies "currentVelocity"
The function signature is (from Unity scripting manual):
static function SmoothDamp (current : Vector3, target : Vector3, ref currentVelocity : Vector3, smoothTime : float, maxSpeed : float = Mathf.Infinity, deltaTime : float = Time.deltaTime) : Vector3
The function uses the "ref" keyword to (apparently) indicate what looks like C-style indirection (i.e., *currentVelocity). The "ref" keyword is also a feature of C# (as well as "out"), which produces the same effect in C#.
However, when I try to write a javascript function using "ref" in the same manner as shown in the unity docs, I get an error: BCE0043: "unexpected token: currentVelocity"
You shouldn't have to use the ref keyword in Unity Script, just pass in the parameter that references it and that should be it.
Answer by Eric5h5 · Feb 07, 2012 at 09:26 PM
You can't specify out or ref variables in functions when using Javascript. You can make use of functions that have ref or out variables that are written in C#, but can't declare them in Javascript. (I hope they add this sometime...it's the one thing about Javascript that I really don't like, and obviously the functionality is already there, so it shouldn't take much to implement it properly.)
Your answer
Follow this Question
Related Questions
Change value of dropdown using JS 2 Answers
Java Script - Out Parameter 1 Answer
Passing a parameter by reference from JavaScript 2 Answers
Setting Scroll View Width GUILayout 1 Answer