Search Unity

no sound on collision??

Discussion in 'Scripting' started by Gabimela, Jan 15, 2020.

  1. Gabimela

    Gabimela

    Joined:
    Oct 3, 2019
    Posts:
    23
    I am trying to create a sound when the player collides with a key and i am hearing nothing. this is what i have

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PickupTrigger : MonoBehaviour
    5. {
    6.  
    7.     public AudioSource keysource;
    8.  
    9.     void Start()
    10.     {
    11.         keysource = GetComponent<AudioSource>();
    12.     }
    13.     void OnTriggerEnter(Collider col)
    14.     {
    15.         // Task 2 part 1 code goes here
    16.         if (col.tag == "Player")
    17.         {
    18.             keysource.Play();
    19.             GameManager.Instance.pickup();
    20.             Destroy(gameObject);
    21.         }
    22.      
    23.     }
    24. }
    25.  
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    I had a whole thing about putting in Debug.Log's in various places to debug it, then I noticed that you're destroying the gameObject, which means the AudioSource is getting destroyed. An AudioSource can't make sound if it no longer exists.

    I recommend using AudioSource.PlayOneShot. Don't have an AudioSource on the pickup at all, just have a reference to the AudioClip itself and pass it into PlayOneShot.
     
  3. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    natteappel likes this.
  4. Gabimela

    Gabimela

    Joined:
    Oct 3, 2019
    Posts:
    23
    I still can't hear anything

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PickupTrigger : MonoBehaviour
    5. {
    6.  
    7.    public AudioClip impact;
    8.    AudioSource keysource;
    9.  
    10.     void Start()
    11.     {
    12.         keysource = GetComponent<AudioSource>();
    13.     }
    14.     void OnTriggerEnter(Collider col)
    15.     {
    16.      
    17.         if (col.tag == "Player")
    18.         {
    19.             keysource.PlayOneShot(impact);
    20.             GameManager.Instance.pickup();
    21.             Destroy(gameObject);
    22.         }
    23.    
    24.     }
    25. }
     
  5. Gabimela

    Gabimela

    Joined:
    Oct 3, 2019
    Posts:
    23
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Oh, that was my mistake. I meant PlayClipAtPoint, not PlayOneShot.
     
  7. Gabimela

    Gabimela

    Joined:
    Oct 3, 2019
    Posts:
    23
    I have put this
    Code (CSharp):
    1. AudioSource.PlayClipAtPoint(keysource, new Vector3(-11.816, 11.093, -155.782));
    but it is telling me that it cannot convert from double to float but those are my coordinates
     
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    There are two data types for decimal-point data, "double" and "float". double supports larger numbers and/or more precision, but uses more memory. Unity uses float everywhere because that's usually as much precision as is needed for games, and memory is generally at more of a premium. In C#, decimal numbers are by default assumed to be doubles. To force it to be a float, you can put "f" at the end of the number: -11.816f, for example. That'll fix the error.

    But, you should not really be typing numbers into the script in this situation. You want to use the object's own position, so just feed that variable straight into the function:
    Code (csharp):
    1. AudioSource.PlayClipAtPoint(impact, transform.position);
    (also, that function wants the clip, not the source)
     
    Joe-Censored likes this.
  9. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I'm a big fan of instantiating an effects GameObject at the point of impact, and then attaching any effects you want to play after the primary GameObject is destroyed to this effects object. In this case an audio source, but you'll often want things like particle effects, point lights, etc, at that same point. These also make good candidates for object pooling.
     
    StarManta likes this.