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

Audio Replace a sound

Discussion in 'Audio & Video' started by Flo05, Mar 18, 2020.

  1. Flo05

    Flo05

    Joined:
    Mar 18, 2020
    Posts:
    2
    Hello. I would like to replace one sound with another in a game (winter resort simulator) and I have no idea how to do it
     
  2. Pandazole

    Pandazole

    Joined:
    Sep 23, 2019
    Posts:
    15
    If you mean to change one sound to another with same audio source, you can use this solution:

    Code (CSharp):
    1. using UnityEngine;
    2. [RequireComponent(typeof(AudioSource))]
    3. public class SoundHandler : MonoBehaviour
    4. {
    5.     [SerializeField] AudioClip[] clips; // drag and add audio clips in the inspector
    6.     AudioSource audioSource;
    7.     void Start()
    8.     {
    9.         audioSource = GetComponent<AudioSource>();
    10.     }
    11.     public void ChangeTheSound(int clipIndex) // the index of the sound, 0 for first sound, 1 for the 2nd..etc
    12.     {
    13.         // use one desired logic
    14.         // this will make only one sound to play without interruption
    15.         audioSource.clip = clips[clipIndex];
    16.         audioSource.Play();
    17.  
    18.         // this will make multiple sound to play with interruption
    19.         audioSource.PlayOneShot(clips[clipIndex]);
    20.     }
    21. }
    22.  
    Screenshot (365).png


    Hope this help, GoodLuck.
     
  3. Flo05

    Flo05

    Joined:
    Mar 18, 2020
    Posts:
    2
    Ok thank you !