I need help debugging this code.

Alright. I CANNOT figure out why this is not working. I have no errors and everything seems to work except this. The variable “acceleration” is supposed to increment each time the function is called (which is every frame) by the value “accel” passed into the function. In my debugging, the logs say that acceleration stay at exactly 0.1 every frame (0.1 is the number passed to accel). I tried isolating acceleration from accel because I thought that maybe every time the function is called it was being reset. However, there is still no change. I’m hoping it’s something simple I’m just not seeing. Thanks for any help. Here is the code:

function MoveToTarget(coordinates : Vector3, accel : float, accuracy : float, reset : boolean)
{
	var distanceToCoordinates : float;
	var firstCall : boolean = false;
	var acceleration : float;
	

	//If this is the first time that this function was called
	//This is needed to make sure we don't reset each variable
	//Every time it's called
	if(!firstCall)
	{
		firstCall = true;
		acceleration = accel;
	}
	
	//Calculates the distance to the given coordinates
	distanceToCoordinates = Vector3.Distance(transform.position, coordinates);
	DebugLog("MissileSpeeder/MoveToTarget/distanceToCoordinates: " + distanceToCoordinates);
	DebugLog("MissileSpeeder/MoveToTarget/acceleration: " + acceleration);
	DebugLog("MissileSpeeder/MoveToTarget/firstCall: " + firstCall);

	if(!reset)
    {
		//Rotates the vehicle toward coordinates
		targetRotation = Quaternion.LookRotation (coordinates - transform.position);
		transform.localRotation = Quaternion.Lerp (transform.rotation, targetRotation, turnSpeed * Time.deltaTime);

		//Moves the vehicle toward the coordinates
		transform.Translate(Vector3.forward * (Time.deltaTime + acceleration));

		acceleration += 0.1;

	}else//Reset all variables back to default for next call
    {
		acceleration = 0;
		firstCall = false;
	}
}

Ok. I’ve found the problem but I don’t know how to fix it. The problem is in line 4. Every time the function is called it resets “firstCall” to false which means that the bit of code in lines 11-15 that is supposed to prevent the values from being reset each time is executing on each call. So I’m back to square one. So here’s my new question. When ever this function is called, more than likely it will be called repeatedly for several frames. All I’m trying to do is pass it several parameters on the first call, but prevent them from being reset on any of the following calls until I choose to reset them. How could I do this?

Without studying the code too closely, I would say move “firstCall” variable outside of the function so that it becomes a member variable of the class.

You need to make the acceleration variable a member of the class and not of the function itself. Unless you are passing a new value through “accel” each time you call it, it will always remain the same. Really both firstCall and acceleration need to be part of the class and not part of the function. The distanceToCoordinates could remain either way.

var distanceToCoordinates : float;
var firstCall : boolean = false;
var acceleration : float;

function MoveToTarget(coordinates : Vector3, accel : float, accuracy : float, reset : boolean)
{
    //If this is the first time that this function was called
    //This is needed to make sure we don't reset each variable
    //Every time it's called
    if(!firstCall)
    {
        firstCall = true;
        acceleration = accel;
    }

    //Calculates the distance to the given coordinates
    distanceToCoordinates = Vector3.Distance(transform.position, coordinates);
    DebugLog("MissileSpeeder/MoveToTarget/distanceToCoordinates: " + distanceToCoordinates);
    DebugLog("MissileSpeeder/MoveToTarget/acceleration: " + acceleration);
    DebugLog("MissileSpeeder/MoveToTarget/firstCall: " + firstCall);
 
    if(!reset)
    {
        //Rotates the vehicle toward coordinates
        targetRotation = Quaternion.LookRotation (coordinates - transform.position);
        transform.localRotation = Quaternion.Lerp (transform.rotation, targetRotation, turnSpeed * Time.deltaTime);
 
        //Moves the vehicle toward the coordinates
        transform.Translate(Vector3.forward * (Time.deltaTime + acceleration));
 
        acceleration += 0.1;
 
    }else//Reset all variables back to default for next call
    {
        acceleration = 0;
        firstCall = false;
    }
}

I think this is what you are looking for. Let me know if it helps.

LordAelfric
Starleaf Labs

Yes! That was the problem. I moved firstCall and acceleration out of the function and now it works! Thanks very much! And you know what, that was kinda obvious. sigh I’ve been coding for too long.