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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Trying to call audio files by name with a static void.

Discussion in 'Audio & Video' started by corlenbelspar, May 9, 2015.

  1. corlenbelspar

    corlenbelspar

    Joined:
    May 1, 2015
    Posts:
    14
    I made the following code so I can do a simple way of calling an audio file by name with "AudioManager.PlaySound();" but the problem is I don't know what to put in the parenthesis. I tried putting jump1 and "jump1" and on the former it says jump1 doesn't exist as an object and the latter says I can't input a string there. What do I put in the parenthesis exactly so I can call sounds by filename?

    using UnityEngine;
    using System.Collections;

    [RequireComponent(typeof(AudioSource))]
    public class AudioManager : MonoBehaviour {

    public static AudioSource audioSource;

    static public void PlaySound(AudioClip clip)
    {
    audioSource.PlayOneShot(clip,1);
    }
    }
     
  2. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,302
    Where are the clips being stored in the scene?

    If they aren't being stored, then you have add them from the inspector, or load them from resources at runtime.

    If they are being stored in the scene, then you need a way to marry the string names with the references to the clips. You can use a dictionary. Then you can use a string argument in your function, and use that string to fetch the clip reference from the dictionary.

    If you are loading from resources, then you are loading them by their file name, so you already have the file name to put in the dictionary.
    If you are storing them another way, then you can fetch the filename with YourAudioClipVariable.name
     
    Last edited: May 9, 2015
  3. corlenbelspar

    corlenbelspar

    Joined:
    May 1, 2015
    Posts:
    14
    I got it working in this fashion I was wanting where it creates multiple audiosources and is supposed to destroy them when they're done playing but it seems to only destroy one if multiple ones play at the same time. How do I get it to cycle through ALL the audiosources and destroy them all when they're done playing?

    using UnityEngine;
    using System.Collections;

    public class SoundManager : MonoBehaviour {

    public static AudioSource audioSource;

    public static void PlaySound(string clip)
    {
    audioSource = GameObject.Find("Player").AddComponent<AudioSource>();
    AudioClip audioClip = (AudioClip)Resources.Load("sounds/" + clip);
    audioSource.bypassListenerEffects = true;
    audioSource.loop = false;
    audioSource.clip = audioClip;
    audioSource.Play();
    }

    void Update()
    {
    if (audioSource == null)
    {
    }
    else
    {
    if (!audioSource.isPlaying)
    {
    Destroy(audioSource);
    }
    }
    }
    }
     
  4. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Please use code tags when posting source code.