Cycling through models on key press?

Hi,
I’ve been checking out the car tutorial that comes with unity and I’ve tweaked it to my liking so I can use it in future games but I’d like to be able to change the body model of it with a keypress, eg. press p for porsche and so on. I’ve looked through the forums and there was a similar topic that I made this script from and attached it to the car gameobject of which the Body (original model) and the porsche model are children.

var modelOne = GameObject.Find("Catamount");
var modelPorsche = GameObject.Find("Porsche");
function Update(){
if Input.GetButtonUp("c")){
modelOne.SetActiveRecursively(true);
modelPorsche.SetActiveRecursively(false);
}
if Input.GetButtonUp("p")){
modelOne.SetActiveRecursively(false);
modelPorsche.SetActiveRecursively(true);
}

}

Now Unity is telling me that you are not allowed to call this function when declaring a variable and since the Porsche model is a child of the car class, it displays both models at the same time. Has anybody done anything like this? Any help would be appreciated. Thank-you,

Anna

Unity has some restrictions on declaring and initializing a member variable in the same instruction: you can use constants or expressions, but not UnityEngine functions or properties. You must declare the variable in one line, then initialize it in a different line (in javascript; in C# and Boo you must initialize it in Awake or Start):

var modelOne: GameObject;
modelOne = GameObject.Find("Catamount");
var modelPorsche: GameObject;
modelPorsche = GameObject.Find("Porsche");

The rest of your code seems ok, and probably will work when you fix the initialization errors.

Oh, I didn’t know that. Anyhow, it works great now, thanks a bunch :smiley: