- Home /
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
Answer by aldonaletto · Nov 21, 2011 at 10:25 PM
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.
Answer by cystemic · Nov 21, 2011 at 10:33 PM
Oh, I didn't know that. Anyhow, it works great now, thanks a bunch :D
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to control a car using joystick 1 Answer
please help looking for animater for FPS 1 Answer
Camera follows not right 0 Answers
Changing variable numbers depending on current player model. 1 Answer