Home > Mobile >  Unity set Script as AudioSource
Unity set Script as AudioSource

Time:06-01

Is there a way in Unity to set a Script as an AudioSource? I have a Script which produces Sound, i now want to Visualize this, but with GetComponent i only get a null value.

I have tried with GetComponent but for that i need to set the Script as the Source, and it already worked with AudioListeners but i need the AudioSource to also visualize it. I have used several online tutorials, but no one uses a Script as Source.

I can only see one solution left, which i want to avoid ,which would be writing the data from the AudioSource in a file, and generating the Visualization that way.

CodePudding user response:

You cannot use a script as an AudioSource. There is already an AudioSource script. If an object contains the AudioSource script, then you are able to access with GetComponent<AudioSource>(). But it ust already be on the object.

CodePudding user response:

Your question is quite ambiguous. you can't use your custom script as AudioSource, as it is a sealed class.

If you want to get access data of the audioClip or audioSource, add AudioSource component to same object in which your script is attached. like: enter image description here

make a public AudioSource variable in your script and assign the AudioSource component, as shown above in screenshot. In this way you will be able to access the data you want in you custom scripts via that variable.

  • Related