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. Dismiss Notice

Question Help with weapon swapping

Discussion in 'Scripting' started by vaqquixx, Sep 22, 2020.

  1. vaqquixx

    vaqquixx

    Joined:
    Jul 29, 2018
    Posts:
    18
    I'm trying to add weapon swapping in my game, it works but i want to only be able to swap weapons if the weapon has an enabled bool called "isCarrying" on it , this script goes through all of the weapons that are child objects of it and enables and disables them, but i only want them enabled if their own "Shooting" script has the "isCarrying" bool enabled, and if not it just skips over onto the next weapon that does. I hope you can understand what I want, all help is appreciated.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class WeaponSwap : MonoBehaviour
    6. {
    7.     public int selectedWeapon = 0;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         SelectWeapon();
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         int previousSelectedWeapon = selectedWeapon;
    19.  
    20.         //scroll up
    21.         if (Input.GetAxis("Mouse ScrollWheel") > 0f)
    22.         {
    23.  
    24.             if (selectedWeapon >= transform.childCount - 1)
    25.                 selectedWeapon = 0;
    26.             else
    27.                 selectedWeapon++;
    28.         }
    29.  
    30.         //scroll down
    31.         if (Input.GetAxis("Mouse ScrollWheel") < 0f)
    32.         {
    33.             if (selectedWeapon <= 0)
    34.                 selectedWeapon = transform.childCount -1;
    35.             else
    36.                 selectedWeapon--;
    37.         }
    38.  
    39.         if(selectedWeapon != previousSelectedWeapon)
    40.         {
    41.             SelectWeapon();
    42.         }
    43.     }
    44.  
    45.     void SelectWeapon()
    46.     {
    47.         int i = 0;
    48.         foreach (Transform weapon in transform)
    49.         {
    50.             if (i == selectedWeapon)
    51.                 weapon.gameObject.SetActive(true);
    52.             else
    53.                 weapon.gameObject.SetActive(false);
    54.             i++;
    55.         }
    56.     }
    57. }
     
  2. Epickidman

    Epickidman

    Joined:
    Sep 13, 2020
    Posts:
    7
    If you want to check the value of variables in another script you can use isWeaponBeingCarried = weapon.gameObject.GetComponent<Shooting> ().IsCarrying; // This sets isWeaponBeingCarried to the value of IsCarrying from the shooting script in the weapon you want to switch to
    If(isWeaponBeingCarried == True)
    //Switch to the weapon
    If you don't understand something or this is not what you want tell me
     
  3. vaqquixx

    vaqquixx

    Joined:
    Jul 29, 2018
    Posts:
    18
    I don't know where to implement that in the code
     
  4. Epickidman

    Epickidman

    Joined:
    Sep 13, 2020
    Posts:
    7
    The new stuff are in lines 32 and 33 and lines 43 and 44
    Make sure that the IsCarrying variable in shooting is a public bool


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

    public class NewBehaviourScript : MonoBehaviour
    {
    public int selectedWeapon = 0;

    // Start is called before the first frame update
    void Start()
    {
    SelectWeapon();
    }

    // Update is called once per frame
    void Update()
    {
    int previousSelectedWeapon = selectedWeapon;

    Debug.Log (selectedWeapon);
    Debug.Log (transform.GetChild (selectedWeapon).name);


    //scroll up
    if (Input.GetAxis("Mouse ScrollWheel") > 0f)
    {

    if (selectedWeapon >= transform.childCount - 1)
    selectedWeapon = 0;
    else
    selectedWeapon++;
    if(transform.GetChild(selectedWeapon).gameObject.GetComponent<Shooting>().IsCarrying == false)
    selectedWeapon++;
    }

    //scroll down
    if (Input.GetAxis("Mouse ScrollWheel") < 0f)
    {
    if (selectedWeapon <= 0)
    selectedWeapon = transform.childCount -1;
    else
    selectedWeapon--;
    if(transform.GetChild(selectedWeapon).gameObject.GetComponent<Shooting>().IsCarrying == false)
    selectedWeapon--;
    }

    if(selectedWeapon != previousSelectedWeapon)
    {
    SelectWeapon();
    }
    }

    void SelectWeapon()
    {
    int i = 0;
    foreach (Transform weapon in transform)
    {
    if (i == selectedWeapon)
    weapon.gameObject.SetActive(true);
    else
    weapon.gameObject.SetActive(false);
    i++;
    }
    }
    }
     
  5. vaqquixx

    vaqquixx

    Joined:
    Jul 29, 2018
    Posts:
    18
    The code doesn't work, I get a the error "Transform child is out of bounds"

    Edit: Idk what happened but the code suddenly works
     
    Last edited: Sep 22, 2020