- Home /
AudioClip in ScriptableObject
Can I play an audio file from a class type that inheriates from ScriptableObject (instead of MonoBehaviour)?
for example:
public class Foo : MonoBehaviour {
private OnCollisionEnter(Collision col) {
AudioClip.PlayClipAtPoint(GetComponent<AudioSource>().clip, transform.position);
}
}
Answer by Mike 3 · Jan 10, 2011 at 07:13 AM
You either need a reference to an AudioSource component (at which point you can call the variable audio, and your above code should work), or you need to use the static functions from AudioSource, i.e. PlayClipAtPoint, though that stops you
What you could do is make a new GameObject from code and attach an AudioSource to it, and then you can just store a reference to that
Thanks for the help mike, I'll let you know if that works
I think what I needed is GetComponent().clip to the respective paramater for the PlayClipAtPoint method.
That then stops it from working on ScriptableObjects though. You could add in a public AudioClip variable to assign the clip to ins$$anonymous$$d if you wanted to keep it as ScriptableObject
I think that I did not Think enough about it. The above works perfectly for not being concerned with each of the collision sound names. (this way I can just attach it to the source of the prefab) and get the component.