Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by asdf123 · Mar 28, 2013 at 02:41 AM · fpschangeweaponswrong

Changing weapons script

I working on a FPS game, and i have a problem to change weapons :s

here is the script:

 public string CurrentWeapon;
     
     public GameObject M9;
     public GameObject G36C;
     
     void Start() {
         CurrentWeapon = "M9";    
     }
     
     public void ChangeWeapon() {
         
         if(Input.GetKey(KeyCode.Q)) {
             CurrentWeapon = "G36C";
         }
         
         if(Input.GetKey(KeyCode.E)) {
             CurrentWeapon = "M9";
         }
         
         if(CurrentWeapon == "G36C") {
             M9.active = false;
             G36C.active = true;
         }
         
         if(CurrentWeapon == "M9") {
             M9.active = true;
             G36C.active = false;
         }
     }

I have attached the script on FPS controller, then i link the weapons to the script. but still dont change weapons :s

P.S = M9 and G36C are weapons.

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 HuskyPanda213 · Mar 28, 2013 at 03:09 AM 1
Share

Add: void Update(){ ChangeWeapon(); }

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by KMKxJOEY1 · Mar 28, 2013 at 03:06 AM

Your not calling the function

Add this:

 Void Update()
 {
 ChangeWeapon();
 }
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 asdf123 · Mar 28, 2013 at 04:03 PM 0
Share

thank you :D

avatar image
0

Answer by Unity_scat · Sep 27, 2015 at 05:01 PM

I made a Javascript script on this. Create two tags called Pistol and Stick, a terrain, an empty game object, two prefabs, attach the Pistol and Stick tags to the prefabs, attach the code to the empty game object, attach the prefabs to the Pistol and Stick prefab slots, and done!

 //Variables
 var Pistol : GameObject;
 var Stick : GameObject;
 var pistolisspawned = true;
 var stickisspawned = false;
 var canswitchtopistol = false;
 var canswitchtostick = true;
     
 function Start () {
     ToPistol();
 }
 
 function Update () {
     if(pistolisspawned == true) {
         canswitchtopistol = false;
         canswitchtostick = true;
     }
     if(stickisspawned == true){
         canswitchtostick = false;
         canswitchtopistol = true;
     }
     WeaponSwitch();
 }
 
 function WeaponSwitch() {
     if (Input.GetKeyDown("q")) {
         ToPistol();
     }
     if (Input.GetKeyDown("e")) {
         ToStick();
     }
 }
 
 function ToPistol () {
     if(canswitchtopistol == true) {
     Destroy (GameObject.FindWithTag("Stick"));
     var clone : GameObject;
     clone = Instantiate(Pistol, transform.position, transform.rotation);
     pistolisspawned = true;
     stickisspawned = false;
     }
 }
 
 function ToStick () {
     if(canswitchtostick == true) {
     Destroy (GameObject.FindWithTag("Pistol"));
     var clone : GameObject;
     clone = Instantiate(Stick, transform.position, transform.rotation);
     stickisspawned = true;
     pistolisspawned = false;
     }
 }
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
avatar image
0

Answer by MorganLee909 · Sep 27, 2015 at 05:02 PM

Also, it would make for shorter, simpler code if you just set the weapons active/inactive with the input. Then you would not even need the currentWeapon variable unless you need it elsewhere in your code. Either way, you wouldn't need those last two if statements.

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
avatar image
0

Answer by PVisser · Aug 27, 2017 at 05:01 AM

Here's my version of this script in C# with some slight adjustments. You're activating and deactivating objects so you'll need a third object as a holder for the script since a deactivated object can't re activate itself. Have the one object you want to start with turned on and the others turned off in the editor.

public class WeaponChange : MonoBehaviour {

 public string CurrentWeapon;        //looks for the name of weapons

 public GameObject M9;               //Add objects "inventory" style
 public GameObject G36C;

 void Start()                        
 {
     CurrentWeapon = "M9";           //what you're holding
 }

 void Update()
 {
     ChangeWeapon();                 //checks for weapon change input
 }

 public void ChangeWeapon()
 {

     if (Input.GetKey(KeyCode.Q))    
     //On Input turn this one ON and others OFF
     {
         CurrentWeapon = "G36C";
         G36C.SetActive (true);
         M9.SetActive (false);
         
         
     }

     if (Input.GetKey(KeyCode.E))    
     //On Input turn this one ON and others OFF
     {
         CurrentWeapon = "M9";
         M9.SetActive(true);
         G36C.SetActive(false);      
         //You can also add code for sound, particles, etc.
         
     }
 }
 }


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

15 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

Related Questions

Player moves faster when fps is higher? 1 Answer

How do I put a delay on a gunshot? 2 Answers

Weapon Possition Move On Sprint 1 Answer

Where is the FPSPlayer Script? 0 Answers

Horror Game AI script recommendation? 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