- Home /
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);
}
This is really confusing, so tell me if you're calling that function with sendmessage, why do you want a return method? Send$$anonymous$$essage doesn't return anything...
Please tell us what exactly you want to do with it, so we can offer you a solution.
some more explanation here: I got 2 scripts A and B. in script A Sendmessage refers to the function movebar in script B and sends a variable called time with it.
the $$anonymous$$ovebar function In script B takes time and uses it to change the speed variable. wich needs to be returned and used later on in script B.
however the speed variable never changes.
Answer by Eric5h5 · Aug 27, 2014 at 04:35 PM
The global "speed" variable is entirely different from the local "speed" variable inside the function. SendMessage can't return any value...hint: **Send**Message. 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.
Answer by Landern · Aug 27, 2014 at 04:03 PM
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);
}
I added float as the return type of the function. Nothing changes however its still not working.
what is calling movebar? is it from Send$$anonymous$$essage?
yes movebar is being called from a different script by Send$$anonymous$$essage
Send$$anonymous$$essage doesn't allow a return of a value. It's void for obvious reasons.