Help returning a true varaible

Hi,
Using an OnTriggerEnter to turn a variable true, I want to be able to alter a returned value, to make a speedboost. As it stands, the if statement doesn’t do anything, following a few other answers on here it looks like it should do.
I’m still fairly new with scripting, thanks in advance for any help.

function OnTriggerEnter(other:Collider)
{
	if(other.gameObject.tag == "SpeedBoost")
	{
		speedboost = true;
		Debug.Log("Hit");
	}
}

function Convert_Miles_Per_Hour_To_Meters_Per_Second(value : float) : float
{
	return value * 0.44704;	
	if(speedboost)
	{
		return value * 2;
	}
}

First your “Convert_Miles_Per_Hour_To_Meters_Per_Second” function of course won’t work the way you have it since you return at the start of the function.

To get it working your would have to do something like this:

function Convert_Miles_Per_Hour_To_Meters_Per_Second(value : float) : float
{
    if(speedboost)
    {
        value *= 2;
    }
    return value * 0.44704;
}

However:

  • Your function name is too long
  • Your function name is wrong!!! because it’s not obvious that it will add a speed boost based on some magic variable.

You should do something like:

function ConvertMPH2MPS(speed : float) : float
{
    return speed * 0.44704;
}

function AddSpeedBoost(speed : float) : float
{
    if(speedboost)
        return speed * 2;
    return speed;
}

And use something like:

xxx = ConvertMPH2MPS(AddSpeedBoost(someValue));

or

xxx = AddSpeedBoost(ConvertMPH2MPS(someValue));

which would be the same since the speed boost is just a factor.

Debug.Log(
will cause a syntax error, so get rid of that to start with.
Does the object entering the trigger have both a rigidbody and the “SpeedBoost” tag?

You exit the method in line 12 with return value * 0.44704;, so the following if(speedboost)… will never execute.
Change the method to

if(speedboost)
{
    return value * 2;
}
else
{
    return value * 0.44704;
}

and it will work.

Rather than bothering with a boolean, why not just make it a float? Then you can do

speedboost =1f;
if(other.gameObject.tag == "SpeedBoost")
    {
       speedboost = 2f;
       Debug.Log("Hit");
    }

and

function ConvertMPH2MPS(speed : float) : float
{
    return speed * 0.44704 * speedboost;
}

Save some typing, and it’s clear - plus you can do things like taper the speedboost over time with a coroutine… Although to be fair ConvertMPH2MPS maybe needs a clearer name in that case, like ‘GetGameSpeed’