- Home /
 
no "pointers" to get directly to a variable passed to a function?
it's hard for me to think of a proper name for this problem. if I get it, then I'll change the title,
the thing is that if I use this function
var delay = 0.3;
function Delay(bool:boolean){
    yield WaitForSeconds(delay);
    bool = !bool;
}
 
               
               on some boolean, to turn it off over time of "delay" seconds, I am not really changing the boolean that I passed. I'm just changing the temporal "bool" variable declared in here.
How to change the value that was passed to that function?
Answer by Mike 3 · Dec 26, 2010 at 10:36 AM
You actually couldn't do it the normal way of using a ref parameter due to how coroutines work
On the other hand, what you could do is pass in an instance of a custom class, which has a bool inside of it:
class BoolWrapper
{
    var bool : boolean;
}
 
               
               Using it:
var wrapper = BoolWrapper();
 
               function Start() { wrapper.bool = false; Delay(wrapper); }
 
               function Update() { Debug.Log(wrapper.bool); //this'll change after 0.3s } 
 
               
               And the modified original coroutine:
var delay = 0.3;
function Delay(boolWrapper:BoolWrapper){
    yield WaitForSeconds(delay);
    boolWrapper.bool = !boolWrapper.bool;
}
 
               
               The class will be passed by reference instead of by value, so when you change the bool inside of it, it's changing the original value
@Petroz: because you get an error if you try. Also, JS can't declare reference variables in functions anyway, though it can use them in C# functions.
Your answer
 
             Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
WaitForSeconds/Yield problem 1 Answer
Delaying through yield 2 Answers
yield in C# doesn't work, not event the sample code? 1 Answer
yield WaitForSeconds 3 Answers