How do I add an audiosource component to a scriptable object?
Thank you
If you mean reference one in a scriptable object, it would need to be on a prefab. Not that this would be particularly useful.
As a rule, assets in your project folders cannot reference objects in scenes.
1 Like
Thats a bit strange use case, normally you have a component that reference a SO. And the component is responsible for directing data from the SO to components in the scene. Here is a audio source example from our game
[CreateAssetMenu(menuName = "AudioConfig/AudioConfig")]
public class AudioConfig : ScriptableObject
{
public AudioSource AudioSourceConfiguration;
public AudioClip[] Clips;
}
public static void PlayRandomDelayedAudio(AudioConfig config, Vector3 position, float delay)
{
Instance.DoPlayDelayedAudio(config.AudioSourceConfiguration, config.Clips.Random(), position, delay);
}
private void DoPlayDelayedAudio(AudioSource prefab, AudioClip clip, Vector3 position, float delay, float playbackTime = 0)
{
if (delay < 0.01f)
PlayAtPoint(prefab, clip, position, playbackTime);
else
queue.Add(SetupAudio(clip, Time.time + delay, prefab, position));
}
edit: Maybe not too clear without context. You point out a AudioSource prefab in the SO
Then you can point out the SO from anywere that references a AudioConfig
1 Like