- Home /
Defining Parameters in a Function
Hi, all! I am not new to giving functions parameters to wrk with, but I was wondering if, besides using a return, I can define a parameter.
Here's an example:
I have a timer. When the timer turns off, I want a bool to turn from false to true. I would like to just be able to call Timer(), and supply it any old boolean I want, and have it toggle it when the timer finishes.
A timer would be a super simple function, but I only want it to return once it finishes.
The problem I see with the return function is this:
I have a timer that counts down three seconds and returns true.
In the start function I have myBool = Timer(3);
Well I don't know if the timer will set the bool to true after three seconds, or if it will be disregarded simply because Start is only checked in the beginning.
Sorry for being so unclear. I hope that maybe I have cleared any confusion with examples.
Thanks! - YA
Well in this case they are. You cannot use "reference" ref parameters in Unity Script.
In C#:
IEnumerator Timer(float seconds, ref bool value)
{
yield return new WaitForSeconds(seconds);
value = true;
}
StartCoroutine(Time(3, ref myBool));
The workaround is to use a class, since classes are always passed by reference.
Btw, as far as i remember generators / iterators can't have ref or out parameters since they are internally classes and the parameters become member variables. So it's not possible to use a ref or out parameter. Passing a class reference will of course work.
So I would make the timer a class function?
No, if you wanted to pass a boolean as a reference in JS, you'd make a class that just contains a boolean, and use that as the boolean ins$$anonymous$$d of a "real" boolean.
Your answer
Follow this Question
Related Questions
Changing Color Based on Z position 2 Answers
Why pass parameters into a function? 2 Answers
How can i make my cube not be a 3D parallelogram when i rotate it under its parent? 1 Answer
Local Variables 2 Answers
Another collision detection issue 1 Answer