How do I make it so the audio can only be played once?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PhoneController : Interactable
{
    public AudioSource audioSource;
   
    public override void OnFocus()
    {
        print("LOOKING AT " + gameObject.name);
    }

    public override void OnInteract()
    {
        print("INTERACTED WITH " + gameObject.name);
        audioSource.Play();
    }

    public override void OnLoseFocus()
    {
        print("STOPPED LOOKING AT " + gameObject.name);
    }
}

You could just Destroy() this script, leaving the audioSource alone to finish playing.

So Destroy(ScriptName)?

Destroy seems a bit hacky to me, I would make a boolean to keep track of whether or not the sound has played and if it has use return; to make sure the code can’t be used again. Alternatively you could make the script disable itself if you don’t have any use for it and want to keep the model in the game world given the naming of your class.

If you put the code right where the .Play() call is, I imagine this would suffice:

Destroy(this);  // make sure we don't trigger anymore audios.

I get your point but that’s code, and code might have bugs! FEAR BUGS! :slight_smile:

If you destroy it you barely add any code (no variable, no state, no if statement, no block) and not only that but you reduced the number of running scripts, making the world a better place. DESTROY ALL THE SOFTWARE THINGS!

LOL you can see though he’s named it ‘phonecontroller’ so I’m assuming he might want to access things again at some point.

1 Like

I used the boolean, it seems to be working and I was hoping to be able to cancel the audio playing if the player wants to mid play, do you know how to do that?

Current Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PhoneController : Interactable
{
public AudioSource audioSource;
public bool AudioPlayed = false;

public override void OnFocus()
{
print("LOOKING AT " + gameObject.name);
}

public override void OnInteract()
{
print("INTERACTED WITH " + gameObject.name);
if(AudioPlayed == false)
audioSource.Play();
AudioPlayed = true;
}

public override void OnLoseFocus()
{
print("STOPPED LOOKING AT " + gameObject.name);
}

public void Update()
{
if(AudioPlayed == true)
return;
}
}

I tried it, but it didn’t work, now the audio doesn’t play at all:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PhoneController : Interactable
{
    public AudioSource audioSource;
    public bool AudioPlayed = false;
   
    public override void OnFocus()
    {
        print("LOOKING AT " + gameObject.name);
    }

    public override void OnInteract()
    {
        print("INTERACTED WITH " + gameObject.name);
        if(AudioPlayed == false)
            audioSource.Play();
            AudioPlayed = true;
           
        if(AudioPlayed == true)
            audioSource.Stop();
            AudioPlayed = false;
    }

    public override void OnLoseFocus()
    {
        print("STOPPED LOOKING AT " + gameObject.name);
    }

    public void Update()
    {
        if(AudioPlayed == true)
            return;
    }
}

I admit, I just searched that particular one up randomly and haven’t used it myself, I did find the syntax though in a video.

Try that and see if it fixes the problem, it doesn’t look too complicated to mess with.