I am wanting my car to behave differently on the terrain to what it does on the car model. I have a script below which disables a script but i am unsure how to modify it. I am wanting the ‘Top Speed’ to Change in my ‘Car’ script on collision with ‘terrain’. Any ideas please?
var shouldBeEnabledOnStart = false;
function Start () {
var scripts = gameObject.GetComponentsInChildren (Car, true);
if (!shouldBeEnabledOnStart) {
for (var tmp : Car in scripts) {
tmp.enabled = false;
}
}
else {
for (var tmp : Car in scripts) {
tmp.enabled = true;
}
}
}
function OnTriggerEnter (terrain : Collider) {
if (terrain.gameObject.tag == "car") {
var scripts = gameObject.GetComponentsInChildren (Car, true);
for (var tmp : Car in scripts) {
tmp.enabled = true;
}
}
}
function OnTriggerExit (terrain : Collider) {
if (terrain.gameObject.tag == "car") {
var scripts = gameObject.GetComponentsInChildren (Car, true);
for (var tmp : Car in scripts) {
tmp.enabled = false;
}
}
}
Hint. Don’t disable gameobjects if you’re using FindObject/GetComponent, etc. It can’t find them, so better to keep a reference to them, if you plan to do that.
Im using the car script from the car tutorial and one of the variables you can set is ‘top speed’. i am wanting the cars top speed to change when it enters a trigger. ie make it act differently on different surfaces. The top speed variable is located within the script called car. if that makes sense?
First thing you do in Start() is disable all the Car scripts from what I can tell.
Is this script a controller script that controls multiple cars?
If you do find the Car component script (call it something else so it doesn’t confict with a class name, like carScript)
It should just then be
carScript.maxSpeed = 50;
Debug.Log() is worth using to find out what is going on.