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 How to make hit sounds not play at the start of the game.

Discussion in 'Getting Started' started by jermainetai0307, Jul 9, 2023.

  1. jermainetai0307

    jermainetai0307

    Joined:
    Nov 10, 2022
    Posts:
    25
    i need to make the hit sounds only play when the target is hit, however right now whenever my game loads my prefabs (the targets) load in and play the hit sound once which results in like 4 hit sounds being played at once, is there a way to make it so that the hit sound doesnt play the first time.
    the code in the script is rlly simple just:
    Code (CSharp):
    1. public class soundeffects : MonoBehaviour
    2. {
    3.     public AudioSource source;
    4.     public AudioClip sfx1;
    5.    
    6.     public void button1()
    7.     {
    8.         source.clip = sfx1;
    9.         source.Play();
    10.     }
    11. }
    upload_2023-7-9_17-10-41.png
     

    Attached Files:

  2. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    483
    Untick "Play on Awake". If that is ticked, then as soon as the GameObject exists it will play the sound.
     
    RichAllen2023 likes this.
  3. jermainetai0307

    jermainetai0307

    Joined:
    Nov 10, 2022
    Posts:
    25
    i have tried that, however that makes it so that the sound doesnt play at all.
    And also i have changed the script, i think i gave the wrong script ytd or something. but here is the actual thing, not much different really
    Code (CSharp):
    1.  
    2.     public AudioSource source;
    3.     public AudioClip clip;
    4.     void OnMouseOver()
    5.     {
    6.         if(Input.GetMouseButtonDown(0))
    7.         {
    8.             Destroy(spherePrefab);
    9.             source.PlayOneShot(clip);
    10.             pointer++;
    11.         }
    12.     }
    13.