save passed boolean parameter?

I have a function, ThrowObject, which is passed a boolean direction.

function ThrowObject(dir : boolean){}

When the direction changes, there is some oscillation before the direction settles out. It appears that this oscillating direction gets continually passed to the function ThrowObject.

Is there a way to capture the “dir” variable that ThrowObject receives so that even if the “dir” varaible changes after ThrowObject is called, the parameter passed to ThrowObject doesn’t change?

“dir” is already being “captured” by ThrowObject, since a boolean is passed by value and not reference. If you just pass the value once instead of continually doing so, then the value inside ThrowObject won’t change since it’s local to ThrowObject.

–Eric

Strange. Inside ThrowObject I have a loop that throws the object in the direction passed to the function ThrowObject. At the start of ThrowObject I print out the direction. Then in the loop I print out the direction and within the loop the direction changes.

Any ideas why the direction would change in the loop?

Can you show us the function? I would assume that you must somehow be setting the variable inside the loop.

The function is quite large, but I don’t change the direction inside the function. I transfer the direction to another variable, dirThrow (which is also not changed in the function), that I use inside the loop as:

function TranslateObject(position : Vector3, weaponNumber : int, weaponGO : GameObject, dir : boolean){

var dirThrow         : boolean = (dir) ? true : false;

and then I use this new variable in the loop:

      for(t = 0; t < numberMoves; t++){
          speedX = offsetX ;
          speedY = Mathf.Exp(-(speedX * t)) ;
          offset = (dirThrow ) ? Vector3(speedX, -speedY, 0) : Vector3(-speedX, -speedY, 0);
          weaponGO.transform.Translate(offset, Space.World);
         Debug.Log("dir = " + dir + ", dirThrow = " + dirThrow);
        yield WaitForSeconds(0.1);
      }

But inside the loop the Debug statement prints out oscillating values for dir and dirThrow. Both of them are both true, then false. It only happens when the GO that calls TranslateObject has just changed direction, otherwise no oscillation of the direction.

Everything you’re doing to try and isolate the value of dir is needless. Bools are always assigned by value, not by reference, so whatever is causing this, it’s not that it’s the same value being referenced by multiple variables.

My next guess would be that TranslateObject() is getting called multiple times. If you call TranslateObject() once with dir=true, and then call it again next frame with dir=false, then you’ll see this oscillation. Each instance of the function will do one iteration of the loop once every 1/10th of a second, but in the opposite direction.

@herufeanor:

I initially thought that TranslateObject was getting called multiple times, so I put a Debug.Log inside TranslateObject and the Debug.Log only prints once:

function TranslateObject(position : Vector3, weaponNumber : int, weaponGO : GameObject, dir : boolean){ 

Debug.Log("TranslateObject started");

var dirThrow         : boolean = (dir) ? true : false;

Huh. Well, you’ve got me stumped then. It’s definitely not a problem of being passed by reference, because even if the bool WAS being passed by reference somehow, your “(dir) ? true : false” assignment would fix that. It might be something funky with multi-threading, but I have very little experience with that, so I can’t help you there.

@ herufeanor:

Yeah, I’m stumped too. That’s why I finally posted. Hopefully someone will have some insight.