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

Searching for a tag then playing a sound

Discussion in 'Scripting' started by AtomicCabbage33, Nov 13, 2016.

  1. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    I have two different empty gameobjects which the currentweapon gets instantiated into, which one is dependent on the gun as they need to be in different positions. I want to search for certain tags within the two empty gameobjects and if an object within them has that tag, play that specific gun sound. There is two problems however, the awp gun does not get instantiated into the correct empty gameobject and the gun sounds do not play.

    WeaponManager script where the gun is assigned to a gun holder (the empty gameobject):

    Code (CSharp):
    1. [SerializeField]
    2.     private Transform tecweaponHolder;
    3.    
    4.     [SerializeField]
    5.     private Transform awpweaponHolder;
    6.  
    7.     [SerializeField]
    8.     private PlayerWeapon primaryWeapon;
    9.    
    10.     [SerializeField]
    11.     private PlayerWeapon secondaryWeapon;
    12.  
    13.     private PlayerWeapon currentWeapon;
    14.     private WeaponGraphics currentGraphics;
    15.    
    16.     private Transform currentHolder;
    17.  
    18.     void Start ()
    19.     {
    20.         currentHolder = awpweaponHolder;
    21.         EquipWeapon(primaryWeapon);
    22.     }
    23.    
    24.     void Update()
    25.     {
    26.         if(Input.GetAxis("WeaponSwitch") >0f)
    27.         {
    28.             currentHolder = tecweaponHolder;
    29.             EquipWeapon(secondaryWeapon);
    30.             Debug.Log("switched weapon");
    31.         }
    32.         else if (Input.GetAxis("WeaponSwitch")<0f)
    33.         {
    34.             currentHolder = awpweaponHolder;
    35.             EquipWeapon(primaryWeapon);
    36.             Debug.Log("switched weapon back");
    37.         }
    38.     }
    39.  
    40.     public PlayerWeapon GetCurrentWeapon ()
    41.     {
    42.         return currentWeapon;
    43.     }
    The PlayerShoot script which searches for the tag and plays the appropriate gun sound:
    Code (CSharp):
    1. [SerializeField]
    2.     private Transform tecweaponHolder;
    3.    
    4.     [SerializeField]
    5.     private Transform awpweaponHolder;
    6.  
    7.  
    8.     void Start ()
    9.     {
    10.         if (cam == null)
    11.         {
    12.             Debug.LogError("PlayerShoot: No camera referenced!");
    13.             this.enabled = false;
    14.         }
    15.  
    16.         weaponManager = GetComponent<WeaponManager>();
    17.     }
    18.  
    19.     void Update ()
    20.     {
    21.         currentWeapon = weaponManager.GetCurrentWeapon();
    22.  
    23.         if (PauseMenu.IsOn)
    24.             return;
    25.  
    26.         if (currentWeapon.fireRate <= 0f)
    27.         {
    28.             if (Input.GetButtonDown("Fire1"))
    29.             {
    30.                 Shoot();
    31.                 foreach (Transform child in tecweaponHolder)
    32.                 {
    33.                     if(gameObject.tag == "Tec")
    34.                     {
    35.                         CmdPlayTecShootSound();
    36.                         Debug.Log("tec sound played");
    37.                     }
    38.                 }
    39.                 foreach (Transform child in awpweaponHolder)
    40.                 {
    41.                     if(gameObject.tag == "Awp")
    42.                     {
    43.                         CmdPlayAwpShootSound();
    44.                         Debug.Log("awp sound played");
    45.                     }
    46.                 }
    47.  
    48.             }
     
  2. edwin_s

    edwin_s

    Joined:
    Mar 10, 2015
    Posts:
    19
    GetCurrentWeapon() returns the value of currentWeapon. That variable is never assigned a value. You do update currentHolder all the time. Maybe you mixed them up?