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

Adding sound to coins and death

Discussion in '2D' started by umaruaruchan, Nov 5, 2019.

  1. umaruaruchan

    umaruaruchan

    Joined:
    Nov 5, 2019
    Posts:
    1
    Hey guys! So I'm working on a project and I needed to play sounds while collecting items and dying on my mini game, but I simply cannot make that work! I tried using many many types of scripts, but nothing seems to really work for me...


    You may notice the scripts are pretty much the same, I simply changed the player tag to ''PickupTrigger'' and the coins and enemies are untagged, but I suppose this is not the best way to do it, but I have no knowledge whatsoever on C#

    This is the script for collecting an item:



    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. using System.Collections;
    4.  
    5. public class Pick : MonoBehaviour
    6.  
    7. {
    8.  
    9.         void OnTriggerEnter2D(Collider2D c)
    10.  
    11.         {
    12.  
    13.           if (!c.gameObject.tag.Equals("PickupTrigger")) ;
    14.  
    15.           {
    16.  
    17.            Destroy(c.gameObject);
    18.  
    19.            }
    20.  
    21.        }
    22.  
    23. }
    24.  
    25.  
    26.  
    27.  
    28. And this is the script for the enemy to kill me:
    29.  
    30.  
    31. [code=CSharp]using UnityEngine;
    32.  
    33. using System.Collections;
    34.  
    35. public class KillPlayer : MonoBehaviour
    36.  
    37. {
    38.  
    39. void OnTriggerEnter2D(Collider2D c)
    40.  
    41.        {
    42.  
    43.         if (!c.gameObject.tag.Equals("PickupTrigger")) ;
    44.  
    45.         {
    46.  
    47.           Destroy(c.gameObject);
    48.  
    49.         }
    50.  
    51.      }
    52.  
    53. }
    54.  
    55. [/B]
     
  2. JosephMGS

    JosephMGS

    Joined:
    Nov 5, 2019
    Posts:
    20
    Hi,

    Your code does not have anything related to Play an AudioClip

    First attach an AudioSource to your gameobject (your player should have a collider with IsTrigger checked to enable OnTriggerEnter2D). It does not need to have a tag. But you need to assign tags to coins and enemies. Let say "Coins" and "Enemies"

    Your Script should be something like this;

    Code (CSharp):
    1. //Drag drop your 2 audio files inside into below array in the Editor
    2. public AudioClip[] sounds;
    3.  
    4.     void Start ()
    5.     {
    6.         myaudiosource = GetComponent<AudioSource>();
    7.     }
    8.  
    9.     private void OnTriggerEnter2D(Collider2D collision)
    10.     {
    11.         if(collision.gameObject.tag.Equals("Coins"))
    12.         {
    13.           myaudiosource.PlayOneShot(sounds[0]);
    14.         }
    15.         else if(collision.gameObject.tag.Equals("Enemies"))
    16.         {
    17.           myaudiosource.PlayOneShot(sounds[1]);
    18.         }
    19.     }
     
    scottiesoreal likes this.