Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 WinteryJungle9 · Feb 19, 2020 at 09:37 AM · booleanswitchingheirarchy

How to switch between only the weapons that the player currently has

I have a weapon switching script which switches between all the weapons that are children of the weapon holder, either when you scroll the mouse wheel or when press the number keys. However, it always switches between all of the weapons and I only want you to be able to switch between the weapons that you currently have. I have a simple inventory script which contains boolean's which store whether or not you have a weapon, like hasPistol, hasMachineGun, hasShotgun etc. so I only want you be able to use a weapon if the corresponding boolean is true. This is something that I think is used in a lot of games so I'm surprised that there is so little information on how to do something like this.

I've a few different methods and haven't had great results:

  1. I made all of the weapons (the ones attached to the player that you use) children of a separate empty object and when you pick up a weapon it makes the usable weapon the child of the weapon holder that has the switching script attached to it. This worked well except it muddled the order of the weapons, if you say picked up the shotgun before the pistol, the shotgun would become the 1st weapon and the pistol would become the 2nd weapon in the hierarchy and obviously this isn't what I want as the pistol should always be the 1st weapon and the shotgun should always be the 3rd weapon. I looked into using setsiblingindex to rearrange the hierarchy but I couldn't get it to work properly

  2. Having a script on each weapon that has a script with a boolean which stores whether you have the weapon, the weapon switching script accesses this script and doesn't activate the object if the boolean is false, this kind of worked but it still cycled through all of the objects, it didn't activating the object if the boolean wasn't true but it was still selected by the weapon switching script so it appears that you aren't using a weapon.

Here is the weapon switching script:

 using UnityEngine;
 
 public class itemSwitching : MonoBehaviour
 {
     public int selectedWeapon = 0;
 
     public static bool usingKnife;
     public static bool usingPistol;
     public static bool usingMachineGun;
     public static bool usingShotgun;
 
     // Start is called before the first frame update
     void Start()
     {
         selectWeapon();
     }
 
     // Update is called once per frame
     void Update()
     {
         int previousSelectedWeapon = selectedWeapon;
 
         if (Input.GetAxis("Mouse ScrollWheel") > 0f)
         {
             if (selectedWeapon >= transform.childCount - 1)
                 selectedWeapon = 0;
             else
                 selectedWeapon++;
         }
 
         if (Input.GetAxis("Mouse ScrollWheel") < 0f)
         {
             if (selectedWeapon <= 0)
                 selectedWeapon = transform.childCount - 1;
             else
                 selectedWeapon--;
         }
 
         if (Input.GetKeyDown(KeyCode.Alpha1))
         {
             selectedWeapon = 0;
         }
 
         if (Input.GetKeyDown(KeyCode.Alpha2) && transform.childCount >=2)
         {
             selectedWeapon = 1;
         }
 
         if (Input.GetKeyDown(KeyCode.Alpha3) && transform.childCount >= 3)
         {
             selectedWeapon = 2;
         }
 
         if (Input.GetKeyDown(KeyCode.Alpha4) && transform.childCount >= 4)
         {
             selectedWeapon = 3;
         }
 
         if (previousSelectedWeapon != selectedWeapon)
         {
             selectWeapon();
         }
 
     }
 
     void selectWeapon()
     {
         int i = 0;
         foreach (Transform weapon in transform)
         {
             if (i == selectedWeapon)
                 weapon.gameObject.SetActive(true);
             else
                 weapon.gameObject.SetActive(false);
             i++;
         }
     }
 }

Your help is very much appreciated :)

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by logicandchaos · Feb 19, 2020 at 01:04 PM

You need a list or array that contains all the weapons your character currently has and no more. when your character gets a new weapon you need to add it to the list if they lose a weapon you need to remove it. You could also use booleans like bool doesHaveShotgun; and keep track of it like that instead.
Also you should use on variable instead of 4 booleans, like in your current setup you could set usingMachineGun and usingShotgun to true. You should make an enum and use a switch statement to prevent this.

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 WinteryJungle9 · Feb 20, 2020 at 09:25 AM 0
Share

Thanks for the reply! I've created a list in the weapon switching script and then if the hasPistol bool is set true, the item is added to the list but this is about as far as I got, the item is added to the bottom of the list where as I need it to to be added to a certain place in it (so the switching order is correct). I haven't had much experience with using lists sorry. Is there a way to add a gameobject to a list at a certain position? so that if you picked up the pistol it would add it to the 2nd position on the list or if you picked up the shotgun it added it to the 4th position on the list.

Thanks!

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

124 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 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 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 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 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 would I make 1 bool true whilst making others false 1 Answer

problem on switching gun (a var don't turn true) 0 Answers

Problem with displaying text 1 Answer

Switching weapons with PUN 2 0 Answers

How to talk about objects in the scene, not prefabs? 0 Answers


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