- Home /
boolean as function parameter/argument
Below is my code. I expect test to be "True" after I call "revert(test)". However, Debug.Log prints out "The value of test in GameManager is False". In the Start() function, if I just write "test = !test" then test would equal to "True". I don't know what's wrong with my code.
#pragma strict
var test : boolean = false;
function revert (a : boolean)
{
a = !a;
}
function Start () {
revert (test);
Debug.Log ("The value of test in GameManager is " + test);
}
function Update () {
}
Answer by meat5000 · May 26, 2014 at 11:01 PM
You do not return anything from your function or modify your test boolean.
test and a are not linked. Variable a exists only within the function.
function revert () { test = !test; }
or
function revert (a : boolean)
{
a = !a;
return a;
}
function Start ()
{
test = revert (test);
//etc
Then how could I modify "test" in my "revert(a : boolean)" function? In my game, I need to write a function that takes in a boolean variable and revert it.
I gave you two ways :D
The first one changes test directly, as its a Class variable and has scope through the whole script.
The second sets up the function variable and in Start() test becomes equal to the return value (output) from the function.
You simply left out your 'return' which gets the output from the function and delivers it to the place the function was called. Then you need a variable to store this function output, where the function was called. I'm guessing you actually know this.
I could be wrong but JS doesn't do pointers and PBR exists through GetComponent, tenuously. But don't take my word for it; I do C and AS$$anonymous$$ mostly. All this JS/US nonsense is a lot newer to me than those :D
Answer by KiraSensei · May 26, 2014 at 11:08 PM
I would write it that way :
#pragma strict
var test : boolean = false;
function revert (a : boolean) : boolean {
return !a;
}
function Start () {
test = revert (test);
Debug.Log ("The value of test in GameManager is " + test);
}
Your answer is correct but I can only vote for one and I vote for the earliest answer. Sorry that I didn't vote for you. Thanks for answering my question.