- Home /
Passing a variable by reference in javascript (using a C# script)
So I need to pass a variable by reference and I'm using Javascript. From this answer I learned that I need to do it in C# if I want it to work in Javascript.
Now the tricky part for me is that I never used C#.
So my attempt was to write the following function in C#:
public bool SelectedBoolean (ref bool selectedVariable){
return selectedVariable;
}
And I would like to use it in this Javascript function:
function AddStep (currentScript : List.<ScriptStep>, selectedBoolean : boolean, result : boolean){
var step = new ScriptStep (currentScript, allScriptsList);
step.stepType = StepType.BooleanChange;
step.stepSelectedBoolean = addBoolean.SelectedBoolean (out selectedBoolean : boolean);
step.stepSelectedBooleanResult = result;
currentScript.Add(step);
}
Trying to use the out is just giving me errors and no idea how I could fix it. Also I realized that since the "selectedBoolean" is going through a Javascript function I guess it becomes a value automatically? Does that mean I need to write the final function in C#?
How would I go about solving this?
Thanks in advance!
Answer by Eric5h5 · Jul 16, 2013 at 04:35 AM
You don't use the "ref" or "out" keywords in Unityscript; they are implied. However, classes are always passed by reference, so instead of messing with C# stuff it's often easier if you just make a custom class for the variable type. e.g.:
class RefBool {
var b : boolean;
function RefBool (b : boolean) {
this.b = b;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613110205im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Variable Not Changing. 0 Answers
InputField in a different scene to Text 1 Answer
Changing Variable OnMouseClick? Help! 1 Answer
Change variable value of a script from another script. both in different Game object 1 Answer
Trivial question for Javascript/Unityscript experts: static variables? 4 Answers