Search Unity

How to change a model ( car or gun)

Discussion in 'Scripting' started by hawx, May 4, 2013.

  1. hawx

    hawx

    Joined:
    Sep 29, 2012
    Posts:
    95
    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):
    1.  
    2. var Car1 : GameObject;
    3. var Car2 : GameObject;
    4. var Car3 : GameObject;
    5. var Car4 : GameObject;
    6.  
    7.  
    8. function Start() {
    9.         Car1.enabled = true;
    10.         Car2.enabled = false;
    11.         Car3.enabled = false;
    12.         Car4.enabled = false;
    13.        
    14. }
    15.  
    16. function Update() {
    17.  
    18.  
    19.  
    20.     if (Input.GetKeyDown(KeyCode.4)) {
    21.         Car1.enabled = !Car1.enabled;
    22.         }
    23.        
    24.     if (Input.GetKeyDown(KeyCode.3)) {
    25.         Car2.enabled = !Car2.enabled;
    26.         }
    27.        
    28.     if (Input.GetKeyDown(KeyCode.2)) {
    29.         Car3.enabled = !Car3.enabled;
    30.          }
    31.        
    32.     if (Input.GetKeyDown(KeyCode.1)) {
    33.         Car4.enabled = !Car4.enabled;
    34.          }
    35.     }
    36.  
    37.  
    38.  
    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.
     
    Last edited: May 4, 2013
  2. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    Seems you're not unenableing them.
    Code (csharp):
    1.  
    2. var cars : GameObject[];
    3.  
    4. function Update(){
    5.      if(Input.GetButtonDown("1"))
    6.           SetCar(4);
    7.      if(Input.GetButtonDown("2"))
    8.           SetCar(3);
    9.      if(Input.GetButtonDown("3"))
    10.           SetCar(2);
    11.      if(Input.GetButtonDown("4"))
    12.           SetCar(1);
    13. }
    14.  
    15. function SetCar( car : int){
    16.      for( i : int = 0; i<cars.Length;i++ ){
    17.          if( i == car )
    18.              cars[i].enabled = true;
    19.          else
    20.              cars[i].enabled = false;
    21.      }
    22. }
    23.  
    Untested! :p

    Jamster
     
  3. hawx

    hawx

    Joined:
    Sep 29, 2012
    Posts:
    95
    Is there any way I can call them straight from the assets?
     
  4. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    I'm not sure what you mean...

    By calling the "SetCar" method you can change the car...
     
  5. hawx

    hawx

    Joined:
    Sep 29, 2012
    Posts:
    95
    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.
     
  6. hawx

    hawx

    Joined:
    Sep 29, 2012
    Posts:
    95
    (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
     
  7. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    Sorry, corrected code is here:

    Code (csharp):
    1. var cars : GameObject[];
    2.  
    3. function Update(){
    4.      if(Input.GetButtonDown("1"))
    5.           SetCar(4);
    6.     if(Input.GetButtonDown("2"))
    7.          SetCar(3);
    8.     if(Input.GetButtonDown("3"))
    9.          SetCar(2);
    10.      if(Input.GetButtonDown("4"))
    11.           SetCar(1);
    12. }
    13.  
    14. function SetCar( car : int){
    15.      for( var i : int = 0; i<cars.Length;i++ ){
    16.          if( i == car )
    17.              cars[i].enabled = true;
    18.          else
    19.              cars[i].enabled = false;
    20.      }
    21. }
    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 :)
     
  8. hawx

    hawx

    Joined:
    Sep 29, 2012
    Posts:
    95
    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.
     
  9. hawx

    hawx

    Joined:
    Sep 29, 2012
    Posts:
    95
    It's saying "Input button 1 isn't setup" and is saying this for any key I re-bind it to.
     
    Last edited: May 4, 2013
  10. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    Oh, sorry, try using Input.GetKeyDown(KeyCode.1) etc instead, my mind's not with me today :p

    Jamster
     
  11. hawx

    hawx

    Joined:
    Sep 29, 2012
    Posts:
    95
    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):
    1. var cars : GameObject[];
    2.  
    3.  
    4.  
    5. function Update(){
    6.  
    7.      if(Input.GetKeyDown(KeyCode.P))
    8.  
    9.           SetCar(4);
    10.  
    11.      if(Input.GetKeyDown(KeyCode.O))
    12.  
    13.          SetCar(3);
    14.  
    15.      if(Input.GetKeyDown(KeyCode.I))
    16.      
    17.          SetCar(2);
    18.  
    19.       if(Input.GetKeyDown(KeyCode.U))
    20.  
    21.           SetCar(1);
    22.  
    23. }
    24.  
    25.  
    26.  
    27. function SetCar( car : int){
    28.  
    29.      for( var i : int = 0; i<cars.Length;i++ ){
    30.  
    31.          if( i == car )
    32.  
    33.              cars[i].enabled = true;
    34.  
    35.          else
    36.  
    37.              cars[i].enabled = false;
    38.  
    39.      }
    40.  
    41. }
    Any help would be appreciated.