I'm slightly new I'm having trouble , I'm trying to make a car(in this instance) change into a different car ( more, change the mesh) I thought I could adapt a camera change code to a model , but I can't. Code (csharp): var Car1 : GameObject; var Car2 : GameObject; var Car3 : GameObject; var Car4 : GameObject; function Start() { Car1.enabled = true; Car2.enabled = false; Car3.enabled = false; Car4.enabled = false; } function Update() { if (Input.GetKeyDown(KeyCode.4)) { Car1.enabled = !Car1.enabled; } if (Input.GetKeyDown(KeyCode.3)) { Car2.enabled = !Car2.enabled; } if (Input.GetKeyDown(KeyCode.2)) { Car3.enabled = !Car3.enabled; } if (Input.GetKeyDown(KeyCode.1)) { Car4.enabled = !Car4.enabled; } } So I'd like to change the model of the car/look of the car, so that the player can ride around in different cars. Any help to do this would be appreciated.
Seems you're not unenableing them. Code (csharp): var cars : GameObject[]; function Update(){ if(Input.GetButtonDown("1")) SetCar(4); if(Input.GetButtonDown("2")) SetCar(3); if(Input.GetButtonDown("3")) SetCar(2); if(Input.GetButtonDown("4")) SetCar(1); } function SetCar( car : int){ for( i : int = 0; i<cars.Length;i++ ){ if( i == car ) cars[i].enabled = true; else cars[i].enabled = false; } } Untested! Jamster
what do I put in the "SetCar" method, there's no option in the editor to define the cars, so how do I reference them? Sorry for the confusion.
(From the code you gave me.) It comes up with multiple errors ; Expected EOF , found "}". Expected }, found "else". Expecting ( , found "SetCar". Unexpected token "if". Unexpected token : for. Sorry , I'm not too good at scripting . Any way can fix this? Thanks
Sorry, corrected code is here: Code (csharp): var cars : GameObject[]; function Update(){ if(Input.GetButtonDown("1")) SetCar(4); if(Input.GetButtonDown("2")) SetCar(3); if(Input.GetButtonDown("3")) SetCar(2); if(Input.GetButtonDown("4")) SetCar(1); } function SetCar( car : int){ for( var i : int = 0; i<cars.Length;i++ ){ if( i == car ) cars[i].enabled = true; else cars[i].enabled = false; } } If you attach it to an object and look in the inspector you'll see the variable "Cars", drag and drop the assets onto it
OK, the assign assets button is now working but I can't seem to change the car, any ideas why? I've assigned a asset to it but when I press a number it doesn't change.
I've had a play with the code and got less errors but I don't know what to do now , it's saying " 'enabled' is not a member of 'UnityEngine.GameObject' at lines 33 and 37. Here's the code : Code (csharp): var cars : GameObject[]; function Update(){ if(Input.GetKeyDown(KeyCode.P)) SetCar(4); if(Input.GetKeyDown(KeyCode.O)) SetCar(3); if(Input.GetKeyDown(KeyCode.I)) SetCar(2); if(Input.GetKeyDown(KeyCode.U)) SetCar(1); } function SetCar( car : int){ for( var i : int = 0; i<cars.Length;i++ ){ if( i == car ) cars[i].enabled = true; else cars[i].enabled = false; } } Any help would be appreciated.