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 taylorsnead · Apr 27, 2011 at 12:15 AM · fpsswitchweaponsswitch objects

What's wrong with my weapon switching/swapping script?

I have looked at the FPS tutorial on Unity's website, but forgot their weapon switching script, and I wanted to attempt creating my own from scratch, but I can't seem to get it to work. I feel like it should work correctly. I haven't tested the weapon slots yet, but I was mostly just trying to get a weapon switching, like swapping current gun for the one on the ground, thing working first:

private var currentWeapon : int; private var slot1 : GameObject; slot1 = GameObject.Find("GunTest"); private var slot2 : GameObject; slot2 = GameObject.FindWithTag("Secondary"); private var slot3 : GameObject; slot3 = GameObject.FindWithTag("Special");

var groundweap : GameObject; //groundweap = GameObject.FindWithTag("Pickupable"); private var playerWeapon : GameObject; playerWeapon = groundweap.GetComponent(PickUp).playerweap; private var nearweapon = false; private var groundWeapon1 : GameObject; groundWeapon1 = slot1.GetComponent(Gun).dropweap; private var groundWeapon2 : GameObject; groundWeapon2 = slot2.GetComponent(Gun).dropweap; private var groundWeapon3 : GameObject; groundWeapon3 = slot3.GetComponent(Gun).dropweap; private var groundpickup = groundweap.GetComponent(PickUp);

         function Update() {

//var gun = currentWeapon.GetComponent(Gun); // Slot key commands if (Input.GetButtonDown("Slot1") || Input.GetButtonDown("Slot2") || Input.GetButtonDown("Slot3")) { SwitchSlot(); }

 if (nearweapon && Input.GetButtonDown("Pickup")) {
 PickupSlot(); }

}

         function SwitchSlot() {

if (Input.GetButtonDown("Slot1") && !slot1.active) { slot1.active = true; slot2.active = false; slot3.active = false; currentWeapon = 1;} if (Input.GetButtonDown("Slot2") && !slot2.active) { slot1.active = false; slot2.active = true; slot3.active = false; currentWeapon = 2;} if (Input.GetButtonDown("Slot3") && !slot3.active) { slot1.active = false; slot2.active = false; slot3.active = true; currentWeapon = 3;} }

         function PickupSlot() {

if (groundpickup.slot==1) { if (slot1==0) { Get();} if (slot1==1) { Drop(1); Get();} } if (groundpickup.slot==2) { if (slot2==0) { Get();} if (slot2==1) { Drop(2); Get();} } if (groundpickup.slot==3) { if (slot3==0) { Get();} if (slot3==1) { Drop(3); Get();} } }

         function OnCollisionEnter(collision : Collision) {

if((collision.gameObject.groundweap) && (nearweapon == false)) { nearweapon = true; } } function OnCollisionExit(collision : Collision) { if((collision.gameObject.groundweap) && (nearweapon == true)) { nearweapon = false; } }

         function Drop(slotdrop : int) {

if (slotdrop==1) { Destroy(slot1); Instantiate(groundWeapon1, transform.position, transform.rotation);} if (slotdrop==2) { Destroy(slot2); Instantiate(groundWeapon2, transform.position, transform.rotation);} if (slotdrop==3) { Destroy(slot3); Instantiate(groundWeapon3, transform.position, transform.rotation);} }

         function Get() {

Instantiate(playerWeapon, transform.position, transform.rotation); }

The PickUp script is just declaring the 2 variables mentioned in this script. I looked through all my scripts w/ weapons and couldn't find anything wrong, but is my human mind compensating for something lacking in my script that Unity just doesn't get?

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

No one has followed this question yet.

Related Questions

FPS Weapon inventory script help 0 Answers

switch weapons script not working? 2 Answers

Help needed in changing weapons 2 Answers

Enabling and siabling first person controller 0 Answers

Swtich weapons problem (attach to bone) 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