Converting the .wav file name in Audio Source to a string
I'm having trouble obtaining the text of the .wav file name in the Audio clip variable under Audio Source. This is the code I have but it keeps saying "UnityEngine.Object[]" instead of the song name.
using UnityEngine;
using System.Collections;
public class Music : MonoBehaviour {
Object[] myMusic; // music array
public string songTitle;
void Awake () {
myMusic = Resources.LoadAll("Music",typeof(AudioClip)); // load music in folder "Resources/Music"
GetComponent<AudioSource>().clip = myMusic[Random.Range(0,myMusic.Length)] as AudioClip;
}
void Start (){
GetComponent<AudioSource>().Play();
songTitle = myMusic.ToString();
Debug.Log ("Song title is " + songTitle);
}
// Update is called once per frame
void Update () {
//if(!GetComponent<AudioSource>().isPlaying)
} // end update
} // end class
Answer by TheDavvyd · Feb 14, 2021 at 12:34 PM
You use myMusic you should get the clip instead. Either save to variable when you get random clip from music or do: sontTitle = GetComponent().clip.ToString(); but you should also cache the AudioSource so you don't constantly do GetComponent<>. Bad performance.
Your answer
Follow this Question
Related Questions
Audio Cutting Out Unexplainably 1 Answer
Audio is way too soft on mobile but alright in Unity Editor! 0 Answers
How to add collection sound to watering can? 0 Answers
How to Send Audio Clip Via Facebook Messenger ? 0 Answers
change speed of audio play 1 Answer