Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Help with multiple weapons switching

Discussion in 'Scripting' started by Brocolli, Apr 11, 2017.

  1. Brocolli

    Brocolli

    Joined:
    Jun 28, 2016
    Posts:
    23
    I have a game with lots of weapons and can switch between them fine using my weapon manager script. However this isn't how I want it to function. However using this way I have free access to all the weapons (I only want melee and pistol to be available from the start), and my other problem is that when I pick up a weapon from a pickup (empty game object on a rotating gun model that makes it active) it adds the picked up weapon to my hands (sets it to active) in addition to my current one (only want it to pick up one, and don't automatically switch to weapons when they are picked up) but I don't know how to do this. A friend says I should use inheritance to make a list of objects but I don't know specifics. Here is the weapon manager script.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class WeaponManager : MonoBehaviour {

    public GameObject melee;
    public GameObject pistol;
    public GameObject railGun;
    public GameObject autoProjectile;
    public GameObject machineGun;
    public GameObject rocketLauncher;
    public GameObject lightningGun;
    public GameObject grenadeLauncher;
    public GameObject shotgun;
    public GameObject autoBounce;

    //use list and sort through based on input?
    //check if the player has picked up the object in inventory
    //Activate ispicked up from other scripts
    //check if right weapon is enabled, all other disabled in update
    //for loop to check this

    //Weapon controller (player) should have an array which contains all the weapon he or she owns,
    //when the player walks over a trigger the trigger will use getComponent to get the Weapon controller
    //from the player and check the public array to see if the weapon is already in there. If the weapon
    //has already been obtained then the weapon does not get picked up and will not dissapear.
    //Switch array with List or Dictionary depending on the situation.
    //You want to loop through the List of weapons the Player owns and check if it's name or id is the same
    //as the weapon the trigger will give to the player.

    //completely different.
    // It's a complex thing to explain but in short you want your weapons to be from the same class (inherited)
    //so that you can make a list of type weapon_base
    //All weapons would then inherit from weapon_base making them accesible from the list of type weapon_base.

    void Start () {
    melee.SetActive (false);
    pistol.SetActive (true);
    railGun.SetActive (false);
    autoProjectile.SetActive (false);
    machineGun.SetActive (false);
    rocketLauncher.SetActive (false);
    lightningGun.SetActive (false);
    grenadeLauncher.SetActive (false);
    shotgun.SetActive (false);
    autoBounce.SetActive (false);
    }

    void Update () {
    if (Input.GetButtonDown ("Melee")) {
    melee.SetActive (true);
    pistol.SetActive (false);
    railGun.SetActive (false);
    autoProjectile.SetActive (false);
    machineGun.SetActive (false);
    rocketLauncher.SetActive (false);
    lightningGun.SetActive (false);
    grenadeLauncher.SetActive (false);
    shotgun.SetActive (false);
    autoBounce.SetActive (false);

    }
    if (Input.GetButtonDown("Pistol")) {
    melee.SetActive (false);
    pistol.SetActive (true);
    railGun.SetActive (false);
    autoProjectile.SetActive (false);
    machineGun.SetActive (false);
    rocketLauncher.SetActive (false);
    lightningGun.SetActive (false);
    grenadeLauncher.SetActive (false);
    shotgun.SetActive (false);
    autoBounce.SetActive (false);
    }
    if (Input.GetButtonDown ("RailGun")) {
    melee.SetActive (false);
    pistol.SetActive (false);
    railGun.SetActive (true);
    autoProjectile.SetActive (false);
    machineGun.SetActive (false);
    rocketLauncher.SetActive (false);
    lightningGun.SetActive (false);
    grenadeLauncher.SetActive (false);
    shotgun.SetActive (false);
    autoBounce.SetActive (false);
    }
    if (Input.GetButtonDown ("AutoProjectile")) {
    melee.SetActive (false);
    pistol.SetActive (false);
    railGun.SetActive (false);
    autoProjectile.SetActive (true);
    machineGun.SetActive (false);
    rocketLauncher.SetActive (false);
    lightningGun.SetActive (false);
    grenadeLauncher.SetActive (false);
    shotgun.SetActive (false);
    autoBounce.SetActive (false);
    }
    if (Input.GetButtonDown ("MachineGun")) {
    melee.SetActive (false);
    pistol.SetActive (false);
    railGun.SetActive (false);
    autoProjectile.SetActive (false);
    machineGun.SetActive (true);
    rocketLauncher.SetActive (false);
    lightningGun.SetActive (false);
    grenadeLauncher.SetActive (false);
    shotgun.SetActive (false);
    autoBounce.SetActive (false);
    }
    if (Input.GetButtonDown ("RocketLauncher")) {
    melee.SetActive (false);
    pistol.SetActive (false);
    railGun.SetActive (false);
    autoProjectile.SetActive (false);
    machineGun.SetActive (false);
    rocketLauncher.SetActive (true);
    lightningGun.SetActive (false);
    grenadeLauncher.SetActive (false);
    shotgun.SetActive (false);
    autoBounce.SetActive (false);
    }
    if (Input.GetButtonDown ("LightningGun")) {
    melee.SetActive (false);
    pistol.SetActive (false);
    railGun.SetActive (false);
    autoProjectile.SetActive (false);
    machineGun.SetActive (false);
    rocketLauncher.SetActive (false);
    lightningGun.SetActive (true);
    grenadeLauncher.SetActive (false);
    shotgun.SetActive (false);
    autoBounce.SetActive (false);
    }
    if (Input.GetButtonDown ("GrenadeLauncher")) {
    melee.SetActive (false);
    pistol.SetActive (false);
    railGun.SetActive (false);
    autoProjectile.SetActive (false);
    machineGun.SetActive (false);
    rocketLauncher.SetActive (false);
    lightningGun.SetActive (false);
    grenadeLauncher.SetActive (true);
    shotgun.SetActive (false);
    autoBounce.SetActive (false);
    }
    if (Input.GetButtonDown ("Shotgun")) {
    melee.SetActive (false);
    pistol.SetActive (false);
    railGun.SetActive (false);
    autoProjectile.SetActive (false);
    machineGun.SetActive (false);
    rocketLauncher.SetActive (false);
    lightningGun.SetActive (false);
    grenadeLauncher.SetActive (false);
    shotgun.SetActive (true);
    autoBounce.SetActive (false);
    }
    if (Input.GetButtonDown ("AutoBounce")) {
    melee.SetActive (false);
    pistol.SetActive (false);
    railGun.SetActive (false);
    autoProjectile.SetActive (false);
    machineGun.SetActive (false);
    rocketLauncher.SetActive (false);
    lightningGun.SetActive (false);
    grenadeLauncher.SetActive (false);
    shotgun.SetActive (false);
    autoBounce.SetActive (true);
    }
    }
    }
     
  2. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Try this. I've cleaned up the code. and added the feature that only allows you to select melee and pistol from the start. you will need to call the pickedUpWeapon() function to enable more weapons.

    Sorry for any typo's I free handed the code. If there are any compile errors. it's most likely a missed Capital some where.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class WeaponManager : MonoBehaviour {
    6.  
    7. //you could remove all these GameObjects with a List,  but for know i'm leaving them in use
    8. public GameObject melee;
    9. public GameObject pistol;
    10. public GameObject railGun;
    11. public GameObject autoProjectile;
    12. public GameObject machineGun;
    13. public GameObject rocketLauncher;
    14. public GameObject lightningGun;
    15. public GameObject grenadeLauncher;
    16. public GameObject shotgun;
    17. public GameObject autoBounce;
    18.  
    19. //new list with all GameObjects
    20. private List<GameObject> weaponList;
    21. private List<GameObject> enabledWeaponList;
    22.  
    23. void Start () {
    24. weaponList = new List<GameObject>();
    25. enabledWeaponList = new List<GameObject>();
    26.  
    27. //add each weapon to the list
    28. weaponList.Add(melee);
    29. weaponList.Add(pistol);
    30. weaponList.Add(railGun);
    31. weaponList.Add(autoProjectile);
    32. weaponList.Add(machineGun);
    33. weaponList.Add(rocketLauncher);
    34. weaponList.Add(lightningGun);
    35. weaponList.Add(grenadeLauncher);
    36. weaponList.Add(shotgun);
    37. weaponList.Add(autoBounce);
    38.  
    39. //start with enabling Melee and pistol
    40. pickedUpWeapon(melee);
    41. pickedUpWeapon(pistol);
    42.  
    43. setActiveWeapon(pistol);
    44. }
    45.  
    46. void Update () {
    47. if (Input.GetButtonDown ("Melee")) {
    48. setActiveWeapon(melee);
    49. }
    50. if (Input.GetButtonDown("Pistol")) {
    51. setActiveWeapon(pistol);
    52. }
    53. if (Input.GetButtonDown ("RailGun")) {
    54. setActiveWeapon(railGun);
    55. }
    56. if (Input.GetButtonDown ("AutoProjectile")) {
    57. setActiveWeapon(autoProjectile);
    58. }
    59. if (Input.GetButtonDown ("MachineGun")) {
    60. setActiveWeapon(machineGun);
    61. }
    62. if (Input.GetButtonDown ("RocketLauncher")) {
    63. setActiveWeapon(rocketLauncher);
    64. }
    65. if (Input.GetButtonDown ("LightningGun")) {
    66. setActiveWeapon(lightningGun);
    67. }
    68. if (Input.GetButtonDown ("GrenadeLauncher")) {
    69. setActiveWeapon(grenadeLauncher);
    70. }
    71. if (Input.GetButtonDown ("Shotgun")) {
    72. setActiveWeapon(shotgun);
    73. }
    74. if (Input.GetButtonDown ("AutoBounce")) {
    75. setActiveWeapon(autoBounce);
    76. }
    77. }
    78.  
    79.  
    80.  
    81. private void setActiveWeapon(GameObject activeWeapon)
    82. {
    83.     for(int i = 0; i < weaponList.Count; i++)
    84.         {
    85.             for(int j = 0; j < enabledWeaponList.Count; j++)
    86.             {
    87.                 if(weaponList[i].name == enabledWeaponList[j].name && enabledWeaponList[j].name == activeWeapon.name)
    88.                 {
    89.                     weaponList[i].SetActive (true);
    90.                 }else
    91.                 {
    92.                     weaponList[i].SetActive (false);
    93.                 }
    94.             }
    95.         }
    96. }
    97.  
    98. //when you pickup your empty gameobject,  run this function to enable the use of the weapon
    99. public void pickedUpWeapon(GameObject weaponPickedUp)
    100. {
    101.     enabledWeaponList.Add(weaponPickedUp);
    102. }
    103.  
    104.  
    105. }
     
    paulbuck86 likes this.
  3. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    the code didn't work correctly. So here is a tested working version

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class WeaponManager : MonoBehaviour
    6. {
    7.  
    8.     //you could remove all these GameObjects with a List,  but for know i'm leaving them in use
    9.     public GameObject melee;
    10.     public GameObject pistol;
    11.     public GameObject railGun;
    12.     public GameObject autoProjectile;
    13.     public GameObject machineGun;
    14.     public GameObject rocketLauncher;
    15.     public GameObject lightningGun;
    16.     public GameObject grenadeLauncher;
    17.     public GameObject shotgun;
    18.     public GameObject autoBounce;
    19.  
    20.     //new list with all GameObjects
    21.     public List<GameObject> weaponList;
    22.     public List<GameObject> enabledWeaponList;
    23.  
    24.     void Start()
    25.     {
    26.         weaponList = new List<GameObject>();
    27.         enabledWeaponList = new List<GameObject>();
    28.  
    29.         //add each weapon to the list
    30.         weaponList.Add(melee);
    31.         weaponList.Add(pistol);
    32.         weaponList.Add(railGun);
    33.         weaponList.Add(autoProjectile);
    34.         weaponList.Add(machineGun);
    35.         weaponList.Add(rocketLauncher);
    36.         weaponList.Add(lightningGun);
    37.         weaponList.Add(grenadeLauncher);
    38.         weaponList.Add(shotgun);
    39.         weaponList.Add(autoBounce);
    40.  
    41.         //start with enabling Melee and pistol
    42.         pickedUpWeapon(melee);
    43.         pickedUpWeapon(pistol);
    44.         pickedUpWeapon(shotgun);
    45.  
    46.         setActiveWeapon(pistol);
    47.     }
    48.  
    49.     void Update()
    50.     {
    51.         if (Input.GetButtonDown("Melee"))
    52.         {
    53.             setActiveWeapon(melee);
    54.         }
    55.         if (Input.GetButtonDown("Pistol"))
    56.         {
    57.             setActiveWeapon(pistol);
    58.         }
    59.         if (Input.GetButtonDown("RailGun"))
    60.         {
    61.             setActiveWeapon(railGun);
    62.         }
    63.         if (Input.GetButtonDown("AutoProjectile"))
    64.         {
    65.             setActiveWeapon(autoProjectile);
    66.         }
    67.         if (Input.GetButtonDown("MachineGun"))
    68.         {
    69.             setActiveWeapon(machineGun);
    70.         }
    71.         if (Input.GetButtonDown("RocketLauncher"))
    72.         {
    73.             setActiveWeapon(rocketLauncher);
    74.         }
    75.         if (Input.GetButtonDown("LightningGun"))
    76.         {
    77.             setActiveWeapon(lightningGun);
    78.         }
    79.         if (Input.GetButtonDown("GrenadeLauncher"))
    80.         {
    81.             setActiveWeapon(grenadeLauncher);
    82.         }
    83.         if (Input.GetButtonDown("Shotgun"))
    84.         {
    85.             setActiveWeapon(shotgun);
    86.         }
    87.         if (Input.GetButtonDown("AutoBounce"))
    88.         {
    89.             setActiveWeapon(autoBounce);
    90.         }
    91.     }
    92.  
    93.  
    94.  
    95.     private void setActiveWeapon(GameObject activeWeapon)
    96.     {
    97.         for (int j = 0; j < weaponList.Count; j++)
    98.         {
    99.             if(weaponList[j] == activeWeapon)
    100.             {
    101.                 for (int i = 0; i < enabledWeaponList.Count; i++)
    102.                 {
    103.                     if(enabledWeaponList[i] == activeWeapon)
    104.                     {
    105.                         weaponList[j].SetActive(true);
    106.                     }
    107.                 }
    108.             }
    109.             else
    110.             {
    111.                 weaponList[j].SetActive(false);
    112.             }
    113.         }
    114.  
    115.     }
    116.  
    117.     //when you pickup your empty gameobject,  run this function to enable the use of the weapon
    118.     public void pickedUpWeapon(GameObject weaponPickedUp)
    119.     {
    120.         enabledWeaponList.Add(weaponPickedUp);
    121.     }
    122.  
    123.  
    124. }
     
  4. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    You could save alot of hassle by using a list.

    E.g.

    Code (CSharp):
    1. [System.Serializable]
    2. Public Struct WeaponData
    3. {
    4. Public Gameobject weapon;
    5. Public String inputKey;
    6. }
    7.  
    8. List<WeaponData> possibleWeapons;
    9.  
    10. for(int i = 0; i < possibleWeapons.Count; i++)
    11. {
    12.      if(Input.GetKeyDown(possibleWeapons[i].inputKey))
    13.      {
    14.           SetActiveWeapon(possibleWeapons[i].weapon);
    15.      }
    16. }
    17.  
    This is just an example, but doing something like this will make it mor data driven and save you changing the code for a new weapons.

    You can add an enabled bool to the data to save having the extra list for enabled weapons aswell.
     
    LiterallyJeff likes this.
  5. Brocolli

    Brocolli

    Joined:
    Jun 28, 2016
    Posts:
    23
    Thanks alot this seems to work really well. Will have to change my othercode to do the picked up function but the only problem is I can use the shotgun. Also when I press a number of a weapon I don't have it empties my hand and I'd rather have it keep the current weapon. But thanks this functionality is really useful for a noob like me
     
  6. Brocolli

    Brocolli

    Joined:
    Jun 28, 2016
    Posts:
    23
    nevermind i found it shotgun is set to active in beginning.
     
  7. Brocolli

    Brocolli

    Joined:
    Jun 28, 2016
    Posts:
    23
    Might be a problem as the thing I am picking up is from a trigger on a gameobject on a model but not code with the actual weapon that will be picked
     
  8. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    remove line 44 - pickedUpWeapon(shotgun);

    edit : I see you saw it.
     
  9. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    If you're doing an OnTriggerEnter() then you should be able to use the code above. You can reference the WeaponManager script.
     
  10. Brocolli

    Brocolli

    Joined:
    Jun 28, 2016
    Posts:
    23
    Another problem. I'm not picking up the actual weapon. I have this script on an empty game object that has the collider of the object on the ground, so if I did get it to call the function it wouldn't recognise it as the weapon would it. plus this code isn't actually on the gameobject on the ground. Sorry to keep bringing up problems

    public GameObject myWeapon;
    public GameObject weaponOnGround;

    private float timer = 0f;

    void Start () {
    myWeapon.SetActive(false);
    }

    void OnTriggerEnter (Collider other) {
    if (other.gameObject.tag == "Player") {
    myWeapon.SetActive(true);
    weaponOnGround.SetActive(false);
    }
    }

    void Update () {
    transform.Rotate (new Vector3 (0, 30, 0) * Time.deltaTime);
    if (!weaponOnGround.activeSelf) {
    timer += Time.deltaTime;
    if (timer >= 10) {
    timer = 0;
    weaponOnGround.SetActive(true);
    }
    }

    }
    }
     
  11. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    I'll look into this tonight and get you some code
     
  12. Brocolli

    Brocolli

    Joined:
    Jun 28, 2016
    Posts:
    23
    Thanks alot for the help
     
  13. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    here are the updates you requested.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class WeaponPickUp : MonoBehaviour {
    5.  
    6.     public WeaponManager weaponMgnr;
    7.     public GameObject myWeapon; // I'm using this as the referance to weapon in hand.
    8.     //public GameObject weaponOnGround; //not needed, this object is it's self
    9.     private bool isviewable = true;
    10.     private MeshRenderer meshRender;
    11.     private Collider myCollider;
    12.     private float timer = 0f;
    13.  
    14.     private void Awake()
    15.     {
    16.         weaponMgnr = GameObject.Find("mgnr").GetComponent<WeaponManager>();
    17.     }
    18.  
    19.     void Start()
    20.     {
    21.         meshRender = GetComponent<MeshRenderer>();
    22.         myCollider = GetComponent<Collider>();
    23.     }
    24.  
    25.     void OnTriggerEnter(Collider other)
    26.     {
    27.         if (other.gameObject.tag == "Player")
    28.         {
    29.             Debug.Log("The Player hit me : " + name);
    30.             toggleThings(false);
    31.             weaponMgnr.pickedUpWeapon(myWeapon);
    32.         }
    33.     }
    34.  
    35.     void Update()
    36.     {
    37.         transform.Rotate(new Vector3(0, 30, 0) * Time.deltaTime);
    38.         if (!isviewable)
    39.         {
    40.             timer += Time.deltaTime;
    41.             if (timer >= 10)
    42.             {
    43.                 timer = 0;
    44.                 toggleThings(true);
    45.             }
    46.         }
    47.     }
    48.  
    49.     void toggleThings(bool setToThis)
    50.     {
    51.         isviewable = setToThis;
    52.         myCollider.enabled = setToThis;
    53.         meshRender.enabled = setToThis;
    54.     }
    55. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class WeaponManager : MonoBehaviour
    6. {
    7.  
    8.     //you could remove all these GameObjects with a List,  but for know i'm leaving them in use
    9.     public GameObject melee;
    10.     public GameObject pistol;
    11.     public GameObject railGun;
    12.     public GameObject autoProjectile;
    13.     public GameObject machineGun;
    14.     public GameObject rocketLauncher;
    15.     public GameObject lightningGun;
    16.     public GameObject grenadeLauncher;
    17.     public GameObject shotgun;
    18.     public GameObject autoBounce;
    19.  
    20.  
    21.     //new list with all GameObjects
    22.     public List<GameObject> weaponList;
    23.     public List<GameObject> enabledWeaponList;
    24.  
    25.     void Start()
    26.     {
    27.         weaponList = new List<GameObject>();
    28.         enabledWeaponList = new List<GameObject>();
    29.  
    30.         //add each weapon to the list
    31.         weaponList.Add(melee);
    32.         weaponList.Add(pistol);
    33.         weaponList.Add(railGun);
    34.         weaponList.Add(autoProjectile);
    35.         weaponList.Add(machineGun);
    36.         weaponList.Add(rocketLauncher);
    37.         weaponList.Add(lightningGun);
    38.         weaponList.Add(grenadeLauncher);
    39.         weaponList.Add(shotgun);
    40.         weaponList.Add(autoBounce);
    41.  
    42.         //start with enabling Melee and pistol
    43.         pickedUpWeapon(melee);
    44.         pickedUpWeapon(pistol);
    45.         //pickedUpWeapon(shotgun);
    46.  
    47.         setActiveWeapon(pistol);
    48.     }
    49.  
    50.     void Update()
    51.     {
    52.         if (Input.GetButtonDown("Melee"))
    53.         {
    54.             setActiveWeapon(melee);
    55.         }
    56.         if (Input.GetButtonDown("Pistol"))
    57.         {
    58.             setActiveWeapon(pistol);
    59.         }
    60.         if (Input.GetButtonDown("RailGun"))
    61.         {
    62.             setActiveWeapon(railGun);
    63.         }
    64.         if (Input.GetButtonDown("AutoProjectile"))
    65.         {
    66.             setActiveWeapon(autoProjectile);
    67.         }
    68.         if (Input.GetButtonDown("MachineGun"))
    69.         {
    70.             setActiveWeapon(machineGun);
    71.         }
    72.         if (Input.GetButtonDown("RocketLauncher"))
    73.         {
    74.             setActiveWeapon(rocketLauncher);
    75.         }
    76.         if (Input.GetButtonDown("LightningGun"))
    77.         {
    78.             setActiveWeapon(lightningGun);
    79.         }
    80.         if (Input.GetButtonDown("GrenadeLauncher"))
    81.         {
    82.             setActiveWeapon(grenadeLauncher);
    83.         }
    84.         if (Input.GetButtonDown("Shotgun"))
    85.         {
    86.             setActiveWeapon(shotgun);
    87.         }
    88.         if (Input.GetButtonDown("AutoBounce"))
    89.         {
    90.             setActiveWeapon(autoBounce);
    91.         }
    92.     }
    93.  
    94.  
    95.  
    96.     private void setActiveWeapon(GameObject activateWeapon)
    97.     {
    98.         for (int i = 0; i < enabledWeaponList.Count; i++)
    99.         {
    100.             if(enabledWeaponList[i] == activateWeapon)
    101.             {
    102.                 for (int j = 0; j < weaponList.Count; j++)
    103.                 {
    104.                     if(weaponList[j] == activateWeapon)
    105.                     {
    106.                         weaponList[j].SetActive(true);
    107.                     }
    108.                     else
    109.                     {
    110.                         weaponList[j].SetActive(false);
    111.                     }
    112.                 }
    113.             }
    114.         }
    115.     }
    116.  
    117.     //when you pickup your empty gameobject,  run this function to enable the use of the weapon
    118.     public void pickedUpWeapon(GameObject weaponPickedUp)
    119.     {
    120.        
    121.  
    122.         if (enabledWeaponList.Contains(weaponPickedUp))
    123.         {
    124.             // already in the list, do nothing
    125.         }
    126.         else //not in the list so add it
    127.         {
    128.             enabledWeaponList.Add(weaponPickedUp);
    129.         }
    130.     }
    131.  
    132.  
    133. }
     
  14. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    and here is my test scene so you can see how i hooked everything up.
     

    Attached Files:

  15. Brocolli

    Brocolli

    Joined:
    Jun 28, 2016
    Posts:
    23
    Got it to work with the railgun thanks so much. Just need to apply it to all my weapons and I'm good to go start making the visuals
     
  16. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    good deal man. glad i could help
     
    Brocolli likes this.
  17. Ajesh_Yohan

    Ajesh_Yohan

    Joined:
    Jun 11, 2016
    Posts:
    5
    hi
    Hi sir
    I need a help in 3rd person shooter
    i am using 3 categories of weapon
    pistol, rifle and launcher
    and each of them have some count of weapons
    while the game starts pistol only be active
    if i pick up a weapon of type 2 it should added to weapon list 2
    and again i am going to pick up a weapon of type 2 it need to pick up the new weapon and drop the old one(replace the weapon in weapon list.
    how?
     
  18. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    is the code from above still relevant?
     
  19. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    @Ajesh_Yohan you should start your own post, don't high jack this thread
     
  20. Deleted User

    Deleted User

    Guest

    I keep getting this "
    NullReferenceException: Object reference not set to an instance of an object
    weaponpickup.Awake ()" and it isn't picking up the object
     
  21. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    The error is simple. You have a variable like a GameObject or something that is Null. You ether didn't set the value in the inspector or you're trying to set the value in code with GetComponent<what ever type you're after>(). and the the game can't the the thing you're looking for, and thus it's null.
     
  22. AlexRassloff

    AlexRassloff

    Joined:
    Nov 7, 2021
    Posts:
    2
    Hi there. I found this very useful. I know this post is slightly old, but I'm just curious, how would one go about modifying this script so that switching weapons is also possible with the scroll wheel? Thanks in advance!
     
  23. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,814
    Steps to success:

    - learn how to read the scroll wheel (google!)

    - decide how to convert continuous motion on the scroll wheel to a discrete intent (eg, how much distance constitutes one move) and implement that conversion (check it with Debug.Log())

    - plumb the detected "change weapon" intent you create above into wherever the current keyboard-based intent is processed.