How to switch weapons?(c#?)

Hey Everybody!

So I’m new to scripting, And I was wondering how you could switch weapons by like pressing 1 or 2 or 3 and so on and so forth.

Is it possible in c#?

If you could get back to me that will be great!
Hint’s and/or examples are greatly appreciated!

Thanks

-Squizy

Everything is possible in Unity! Don’t forget that :smiley:

Since your new Ill go over a pretty basic way. Add some sort of weapons, plural because it manages all weapons the player has, script to your player to handle all the attached weapons.

Give it something like this:

public int currentWeapon
public transform[] weapons;

Now in the editor you should be able to drag and drop the weapon objects into the array after settings its size to what you need and choose which is the current weapon, remember 0 is the first value in the array. Im assuming each weapon is its own contained object and will work on its own if its attached to the player? This is an entirely different question haha.

So now in the weapons script Update function do your input handling such as:

if(Input.getKeyDown(KeyCode.Alpha1)) {
   changeWeapon(1);
}

etc. and do this for all the weapon numbers 0 - x weapons. And then somewhere else:

public void changeWeapon(int num) {
    currentWeapon = num;
    for(int i = 0; i < weapons.Length; i++) {
        if(i == num)
            weapons*.gameObject.SetActive(true);*

else
weapons*.gameObject.SetActive(false);*
}
}
What this is doing is as you press a number it disables all other weapons and enables the one you are selecting to. You can change it from simple disabling them to maybe play an animation of whatever but hopefully this is a start. It also means you can use the scroll wheel if you want such as if you scroll up call changeWeapon(currentWeapon + 1) etc. You should probably also call changeWeapon(0) or to whatever starting weapon you want so it disables the rest at the start. Put this call in the Start function of the weapons script.
Play around with it and have fun :smiley:
Also note that I havnt test these, just wrote them off the top of my head. Refer to Unity’s C# docs for syntax too!

Here is another script example in C#:

using UnityEngine;
using System.Collections;

public class Weapons : MonoBehaviour {

	public GameObject[] weapons; // push your prefabs

	public int currentWeapon = 0;
	
	private int nrWeapons;
	
	void Start() {
		
		nrWeapons = weapons.Length;
		
		SwitchWeapon(currentWeapon); // Set default gun
	
	}
	
	void Update () {
		for (int i=1; i <= nrWeapons; i++)	{ 
			if (Input.GetKeyDown("" + i)) {
				currentWeapon = i-1;
				
				SwitchWeapon(currentWeapon);
			
			}
		}		

	}
	
	void SwitchWeapon(int index) {

		for (int i=0; i < nrWeapons; i++)	{
			if (i == index) {
				weapons*.gameObject.SetActive(true);*
  •  	} else {* 
    

_ weapons*.gameObject.SetActive(false);_
_
}_
_
}_
_
}*_

}

something like this might work better, let me know what you guys think.

   if(Input.GetKey(KeyCode.Tab))
 {
 CycleWeapons();
 }
 
 function CycleWeapons ()
 {
 if (Shield)
 {
 melee.setactive
 }
 if (bow)
 {
 shield.setactive
 }
 if (melee)
 {
 shield.setactive
 }

Hello, I do not know if this works, I just wrote this code directly into the forums.

using UnityEngine;
using System.Collections;

public class WeaponSwitch : MonoBehaviour
{

public gameObject weapon1;
public gameObject weapon2;


void Update ()
{
    if (Input.GetButtonDown("1"))
    {
        weapon1.enabled = true;
        weapon2.enabled = false;
    }
    if (Input.GetButtonDown("2"))
    {
        weapon1.enabled = false;
        weapon2.enabled = true;
    }
}

}