Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by SirMcsquizy · Dec 04, 2013 at 08:45 PM · weapon

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image flaviusxvii · Dec 05, 2013 at 05:29 PM 1
Share

http://lmgtfy.com/?q=unity3d+switch+weapons

4 Replies

· Add your reply
  • Sort: 
avatar image
9

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!

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SirMcsquizy · Dec 04, 2013 at 11:14 PM 1
Share

Thanks So $$anonymous$$UCH! I really appreciate it! I Seriously do!

avatar image xt-xylophone · Dec 04, 2013 at 11:48 PM 0
Share

The other guys script is essentially the same thing but a complete script over snippets

avatar image TimeToGame · Feb 29, 2016 at 02:41 AM 0
Share

this is 3 years later and you helped me a lot :P

avatar image PZ4_Bailey · Jun 21, 2018 at 06:28 AM 0
Share

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?

avatar image KUFgoddess · Nov 08, 2018 at 10:22 AM 0
Share

Lol 5 years later and its still useful thanks!

avatar image
5

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);
             }
         }
     }
     
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xt-xylophone · Dec 04, 2013 at 11:02 PM 0
Share

I guess this is the most common way eh? :D

avatar image SirMcsquizy · Dec 05, 2013 at 05:16 PM 0
Share

Heyo!

So I tried out your code, and whenever I switch the weapon, it doesn't show up?

Why is that

avatar image SirMcsquizy · Dec 05, 2013 at 05:24 PM 0
Share

Nvm I got it!

avatar image Dj3vilp · Oct 20, 2014 at 07:24 PM 0
Share

Having same issue i try out ur code n it doesn't show my objects

avatar image
0

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
  }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image J-R-Wood · Sep 09, 2017 at 08:58 PM 0
Share

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; } }

avatar image
0

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;
     }
 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to swap weapons easily? 1 Answer

Adding A Script To A Scene 2 Answers

How to fly with cheat code ? 0 Answers

How to toggle a material property in code? 0 Answers

Putting variable into new Unity GUI InputField and then Acessing that variable 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges