return doesnt return a value.

newbee here with a question

So I call this function from another script using sendmessage and inject a value named time. time is used to calculate the speed. Speed is used in the same script. However function movebar never seems to return speed. Debug.log shows me that speed is = 1 for instance, but outside the function it remains 0.

what am I doing wrong.

 var speed : float;

function movebar (time :float) : float
{
var speed :float = 0; 
speed = 1 / time;
Debug.Log(speed);
Debug.Log(time);
return speed;
}

function Update () {
var step = speed * Time.deltaTime;
loadingbar.position = Vector3.MoveTowards(bar.position, bartarget.position,step);
}

Where is your return type from the function/method?

var speed : float;
 
function movebar (time :float) : float {
	var speed :float = 0; 
	speed = 1 / time;
	Debug.Log(speed);
	Debug.Log(time);
	return speed;
}
 
function Update () {
	var step = speed * Time.deltaTime;
	loadingbar.position = Vector3.MoveTowards(bar.position, bartarget.position,step);
}

The global “speed” variable is entirely different from the local “speed” variable inside the function. SendMessage can’t return any value…hint: SendMessage. It’s one-way only. If the function is supposed to change the global “speed” variable, then it should use that rather than declaring a new local variable.