Collider + multiple sounds script


I am working on a project that mainly needs to have 4-5 objects, each with a different sound track on them, that will play upon touch (collision) with a first person character walking around.

Since all 5 sounds will be connected to each other (one will be bass, one vocals, one guitar etc.) and they actually need to play synchronized , I need them all to play on awake, but muted. When the character collides with an objects, the sound that is attached to that object can just un-mute.

I am an architect, I designed an amazing geometry and need an interactive way to display it.
I have very very limited coding skills and even after looking at unity documentation, still don’t have an exact idea how the script would work!

Any help would be enormously appreciated!

Thanks for the explanation, now I see what you want.

It is actually pretty easy to do this: you just need a script that sets volume to 0 in the beginning, and to 1 (or whatever you would like) when there is a collision with a specific object. For the collision to work, you also need to set up the objects correctly (at least one has to have a rigidbody). For example, your player would have a collider (CapsuleCollider or CharacterController) and a rigidbody (probably set to kinematic), while the sound objects just a collider, like a BoxColldier or SphereCollider.

Here is the script. I will also try to explain what each line does so you can learn from it and become better at programming :wink:

using UnityEngine;

// make sure the game object you attach it to has an AudioSource on it
// create a script that you can attach to a game object
public class SetVolumeOnCollision : MonoBehaviour {

    // the volume to set when collided; can be set in the Editor; 
    // not necessarily needed, but it will make the component more versatile
    [SerializeField] private float volumeOnCollision = 1;

    // the tag to which it should react; also not essential, but nice to have
    // make sure you set the tag of your colliding object if you do use it!
    [SerializeField] private string tagToReactTo = "Player";

    // the audio source you want to control
    private AudioSource audioSource;

    // this is called in the first moment, even before sounds start to play
    private void Awake () {
        // get the audio source to control that is on this game object
        audioSource = GetComponent<AudioSource>();
        // set volume to 0, muting it; you can also do this in the Editor
        audioSource.volume = 0;
        // make sure it wil start to play; this as well can be set in the Editor
        audioSource.playOnAwake = true;

    // this function is called on the frame when something collides with this game object
    private void OnCollisionEnter (Collision collision) {
        // check the tag; remove this if you don't want it
        if (collision.collider.CompareTag(tagToReactTo))
            // set the desired voluem; just replace it with 1 if you don't want to use desired volume
            audioSource.volume = volumeOnCollision;