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

Coins pickup sound

Discussion in '2D' started by zhpn4k, Nov 15, 2014.

  1. zhpn4k

    zhpn4k

    Joined:
    Nov 14, 2014
    Posts:
    4
    I already have a coin prefab and coin spawn prefab. I want that coins wil have the sound effect when I pick up them. I wrote the script and I apply this for the coin prefab:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(AudioSource))]
    5.  
    6. public class PickupCoinScript : MonoBehaviour {
    7.  
    8.     HUDScript hud;
    9.  
    10.     public AudioClip impact;
    11.  
    12.     void OnTriggerEnter2D(Collider2D other)
    13.     {
    14.         if(other.tag == "Player")
    15.         {
    16.             hud = GameObject.Find("Main Camera").GetComponent<HUDScript>();
    17.             hud.IncreaseCoin(1);
    18.             audio.PlayOneShot(impact, 0.7F);
    19.             Destroy(this.gameObject);
    20.         }
    21.     }
    22. }
    But when i start playing, sound play when coins spawn, not when I pick up them.. How to solve this problem? Where I make a mistake? P.S. sorry for bad English
     
  2. valhalla_cats

    valhalla_cats

    Joined:
    Jan 27, 2013
    Posts:
    16
    Try to replace:

    audio.PlayOneShot(impact, 0.7F);

    with

    AudioSource.PlayClipAtPoint(impact, transform.position);
     
    dreamsniper and zhpn4k like this.
  3. zhpn4k

    zhpn4k

    Joined:
    Nov 14, 2014
    Posts:
    4
    thanks! it's working :)
     
    dreamsniper likes this.
  4. valhalla_cats

    valhalla_cats

    Joined:
    Jan 27, 2013
    Posts:
    16
    You're welcome ;)