what Javascript Code will i need for this?

I am working on a Truck Driving game project for a Class and there is a gate that is supposed to refuel the truck however the thing is i need to know what im missing to make this work

function OnTriggerExit(hitWhat : Collider){

if(hitWhat.gameObject.name == "gate") {

var myVariable : GameObject = GameObject.Find("PreCap");
var myChildVariable : GameObject = myVariable.Find("Parent_Cap");
myChildVariable.animation.Play("CapAnime");
		Destroy(hitWhat.gameObject);
		}
		
else if(hitWhat.gameObject.name == "gateE"){;
		Application.LoadLevel ("Truck_GameOver");
		}
		
else if(hitWhat.gameObject.name == "gateF"){;
		var fuel = 100;
		}
	}

any suggestions would be appreacated

1 Answer

1

The code seems ok except for the last if : it assigns 100 to a temporary variable named fuel, which will be discarded as soon as the function ends. fuel should be a member variable like this:

var fuel: float; // declare fuel outside any function

function OnTriggerEnter(hitWhat : Collider){
    ...
    else if(hitWhat.gameObject.name == "gateF"){ // there's no ; here!
        fuel = 100;
    }
}

there is a var fuel : fuel float; far above in the script the full thing was to long to slap in but thank you that helps alot

If already there's a fuel variable, just remove the keyword var before fuel = 100;. The keyword var declares a temporary variable, which hides any other instance previously declared.