- Home /
how to change model via script?
Hello
I was wondering how to change the model in my game. What I am specifically trying to do is change the model of the car which is being used (its a racing game) depending on a variable. For example if my variable is 1 then a certain car (For example, an SUV) but if it is 2 then it is a sports car. I have searched online but have had great trouble changing the model via script.
Thank you very much in advance
Answer by fafase · Jan 04, 2014 at 10:26 AM
Store all the models in an array and use the variable to get the model:
int modelNumber;
public GameObject [] modelArray;
GameObject currentModel;
void Start()
{
// modelNumber is given a value based on the player choice
// I leave that to you since I do not know how you do that
currentModel = modelArray[modelNumber];
}
void Update()
{
// Do stuff with your model
currentModel.transform.Translate(forward);
}
There could be other ways where the array is not kept all over the game since you only need to access the model in the Start but this one may be easier on you to understand right away.
For some reason I get UCE0001: ';' expected. Insert a semicolon at the end. for two of the lines. Probably just a very simple mistake made by me. I think it may be because my code is in Javascript
private var modelNumber:int;
var modelArray : GameObject [];
private var current$$anonymous$$odel : GameObject;
then the void $$anonymous$$ethod -> funtion $$anonymous$$ethod
it works perfectly in changing the model that current$$anonymous$$odel is. But just as a question how can I make the car that is displayed on screen be the model that is current$$anonymous$$odel