- Home /
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
Answer by xt-xylophone · Dec 04, 2013 at 10:49 PM
Everything is possible in Unity! Don't forget that :D
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[i].gameObject.SetActive(true);
else
weapons[i].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 :D
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!
Thanks So $$anonymous$$UCH! I really appreciate it! I Seriously do!
The other guys script is essentially the same thing but a complete script over snippets
Sir i tried your method but when i disable an re-enable a weapon then it loses Animator state so it makes my animations look weird. Any solutions?
Answer by nastasache · Dec 04, 2013 at 10:53 PM
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[i].gameObject.SetActive(true);
} else {
weapons[i].gameObject.SetActive(false);
}
}
}
}
Heyo!
So I tried out your code, and whenever I switch the weapon, it doesn't show up?
Why is that
Having same issue i try out ur code n it doesn't show my objects
Answer by J-R-Wood · Sep 09, 2017 at 07:51 PM
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
}
i had to end up doing this because the previous code idea switched everything at once because it read everything at once and so it was all applied :p function CycleWeapons () { Switch-=1; if (Switch <=0) { Switch =4; } if (Switch==1) { bow = true; melee = false; build = false; shield = false; } if (Switch==2) { bow = false; melee = true; build = false; shield = false; } if (Switch==3) { bow = false; melee = false; build = true; shield = false; } if (Switch==4) { bow = false; melee = false; build = false; shield = true; } }
Answer by Cubemann · Apr 19, 2021 at 09:02 PM
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;
}
}
}