- Home /
Unity4, Unityscript added "ref" or "out" keyword?
Attention, advanced Unity developers!!
As you already know, Unityscript does not support passing by ref to functions
Does anyone know,
Have Unity recently added this to Unityscript - perhaps in U4 ??
I tried all the obvious keywords (ref, out, etc)
It is undocumented so maybe they have added it, but I just don't know the keyword.
Cheers!!
If they'd been added, it would have been in the release notes.
Answer by YM_Industries · Nov 19, 2012 at 11:45 AM
Unityscript does not have any way for passing parameters by reference.
If you must have By Ref arguments, C# supports them.
EDIT: Actually, on further investigation, there may be support for these. The built-in function Physics.Raycast() can be in the form
static function Raycast (origin : Vector3, direction : Vector3, out hitInfo : RaycastHit, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : boolean
Notice the 'out hitInfo' argument. This appears to be ByRef. I have no idea how this can be achieved in a custom function, but it does appear to be possible.
EDIT 2: Eric5h5 has kindly offered some clarification in the comments, I'll sum it up here: If you define your function in JS/US, it cannot utilise ByRef parameters. However, if your function is defined in C#, you can call it from JS/US with Out parameters that are ByRef.
You can use ref and out parameters in JS, but you can't declare them in functions. This means you can have functions in C# scripts which use ref or out parameters and use them in JS with no issues.
No, they are declared in the C# function. There is no mechanism for declaring ref or out variables in JS.
No, aside from variables which are always passed by reference anyway, such as arrays or classes. Structs and primitive types are always by value.
Your answer
Follow this Question
Related Questions
Long-term use of ref/out? 1 Answer
Your "own" structs ... are they indeed passed by value? 1 Answer
NullReferenceException: Object reference not set to an instance of an object - MonoBehaviour 2 Answers
Is it possible to pass a variable by reference in javascript 1 Answer
Pass class and component reference from inside class 1 Answer