[Solved] How to get object instance from creation

    public void CreateRandomAudioSource(out AudioSource source)
    {
        AudioSource randomAudioSource = player.AddComponent<AudioSource>();
        randomAudioSource.spatialBlend = randomAudioSpacial;
        randomAudioSource.reverbZoneMix = randomAudioReverb;
        randomAudioSource.volume = randomAudioVolume;
        randomAudioSource.loop = false;
        source = randomAudioSource;
    }

    IEnumerator DestroyAudioSource(AudioSource source)
    {
        yield return new WaitForSeconds(source.clip.length);
        Destroy(source);
    }

So I want to use the functions above to create and destroy an AudioSource and I’m pretty sure I’m supposed to use an out variable in the creation function to get the specific instance to the Destroy coroutine, I just don’t get how to do that.

You’re not sure how to send the out parameter in the method call, you mean? It might look something like:

AudioSource audioSource;
CreateRandomAudioSource(out  audioSource);
// now 'audioSource' references the newly added one

Math.Abs(source.clip.length / source.pitch) is the correct time to wait…for a pitch that is not zero.
You might want some pitch variation in your audio.

There’s no need for an out parameter, just return it the normal way:

public AudioSource CreateRandomAudioSource()
{
    AudioSource randomAudioSource = player.AddComponent<AudioSource>();
    // set some additional values
    // ...
    return randomAudioSource;
}

Then, you can do this:

var audioSource = CreateRandomAudioSource();
StartCoroutine(DestroyAudioSource(audioSource));

However, the coroutine doesn’t do anything but waiting. If you don’t need additional control, you can as well just Destroy with a delay:

var audioSource = CreateRandomAudioSource();
Destroy(audioSource, audioSource.clip.length); // you might wanna take pitch into account, like recommended by NA-RA-KU

Perhaps the randomly generated sources will always be destroyed, you could even put it into the method:

public void CreateRandomAudioSource()
{
    AudioSource audioSource = ...
    // set some additional values
    // ...
    Destroy(...); // like above
}

Thanks for the help guys.

I implemented the out solution at first but ended up going for this:

public AudioSource CreateRandomAudioSource()
    {
        AudioSource randomAudioSource = player.AddComponent<AudioSource>();
        randomAudioSource.spatialBlend = randomAudioSpacial;
        randomAudioSource.reverbZoneMix = randomAudioReverb;
        randomAudioSource.volume = randomAudioVolume;
        randomAudioSource.loop = false;
        return randomAudioSource;
    }


public void PlaySound(AudioClip clip)
    {
        AudioSource mySource = CreateRandomAudioSource();
        mySource.clip = clip;
        mySource.Play();
        Destroy(mySource, Mathf.Abs(mySource.clip.length / mySource.pitch));
    }

Works great and I can add in pitch variation with ease, thanks again for broadening my coding skills :slight_smile:

1 Like

Good stuff. Glad you got a working solution :slight_smile:

Don’t get me wrong, but is it really necessary to add these posts everywhere? I can understand it’s very polite, but it unnecessarily bumps all the threads once more even though they’re solved. Would you mind just hitting the like button instead?

It’s just a habit to which I’ve grown accustomed. :slight_smile: It’s polite, social, completing a conversation/partial one.
I suppose the bumping isn’t the greatest. I’ll try to keep it down :wink: