Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

how would i make a weapon switching script

Discussion in 'Scripting' started by Darkdragon0011, Jul 8, 2015.

  1. Darkdragon0011

    Darkdragon0011

    Joined:
    Apr 27, 2015
    Posts:
    35
    I'm making a fps and i can't get this to work

    Code (CSharp):
    1.   var weapons : GameObject[];
    2. var weapon1 : GameObject;
    3. var weapon2 : GameObject;
    4. var weapon3 : GameObject;
    5.    
    6.  
    7. function Start(){
    8.      weapon1.SetActive(true);
    9.      weapon2.SetActive(false);
    10.      weapon3.SetActive(false);
    11. }
    12.      var currentWeapon = 0;
    13.    
    14.      function Update() {
    15.          if(Input.GetButton("Switch Key") ) {
    16.            
    17.              weapons[currentWeapon].SetActive(false); // Deactivate the current weapon
    18.            
    19.              currentWeapon ++; // Change weapon
    20.            
    21.              // Make sure that the value of current weapon is no bigger than the amount of weapons
    22.              if(currentWeapon >= weapons.Length){
    23.                  currentWeapon = 0;
    24.              }
    25.            
    26.              weapons[currentWeapon].SetActive(true); // Activate the new weapon
    27.            
    28.          }
    29.      }
    30.      
    where did i go wrong
     
  2. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Are you adding weapons to the array?

    Try doing something like this :

    Code (JavaScript):
    1. var weapons : GameObject[]; //add all your weapons here
    2. var index : int;
    3.  
    4. function Update () {
    5.         if(Input.GetKeyDown(KeyCode.Q)){  //Q to switch weapons
    6.             index++;
    7.             if(index >= weapons.Length){
    8.                 index = 0;
    9.             }
    10.  
    11.             SwitchWeapon(index);
    12.         }
    13.     }
    14.  
    15. function SwitchWeapon(weaponIndex : int){
    16.         for(var i : int = 0; i < weapons.Length; i++){
    17.             if(i == weaponIndex ){
    18.                 weapons[i].SetActive(true);
    19.             }
    20.             else{
    21.                 weapons[i].SetActive(false);
    22.             }
    23.         }
    24.     }
     
    Last edited: Jul 8, 2015
  3. Darkdragon0011

    Darkdragon0011

    Joined:
    Apr 27, 2015
    Posts:
    35
  4. Darkdragon0011

    Darkdragon0011

    Joined:
    Apr 27, 2015
    Posts:
    35
    umm... it dint work
     
  5. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    It should be working. I updated the code a little (got rid of an unnecessary "}")

    Whenever you press Q, it will deactivate all other weapons in the array other than your current weapon (index).
     
  6. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    Your original code is on the right track.

    However, unless you are putting weapons that are in the scene into those slots then it will fail. If you are just putting prefabs in those slots then you have to create them in Start(), store the references as the "real" weapons, and put them where they should go, then do everything on those weapons instead of the prefab references.

    Also you need the switch weapon routine to check to see if the last weapon is current, then roll back to the first one if so.