How do i use PlayOneShot() on Unity?

Viewed 2421

I've been trying to use this function to trigger an sound when entering a trigger but i can't make it work. I've read and read my code and nothing seems off, looks exactly like the documentation says so i just can't figure out my error.

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

public class footsteps_script : MonoBehaviour
{

    public GameObject soundObject;
    public GameObject trigger;
    private AudioClip clip;
    public bool nextTrig;

    void Start()
    {
    nextTrig = false;
    clip = soundObject.GetComponent<AudioClip>();
    }

    void OnTriggerEnter ()
    {

        if (Bunny_script.nextTrig == true)
        {
        
            soundObject.GetComponent<AudioSource>().PlayOneShot(clip);
        nextTrig = true;
        trigger.SetActive(false);
        }
    }



  

    
}

The AudioSource is atached to another object. The trigger is supposed to play the sound after another event happens. The trigger part works fine because nextTrig is set to true like intended, but the sound doesn't play. Also, the sound itself works fine too and with a nice volume.

1 Answers

it does not work because, what is this ?

 clip = soundObject.GetComponent<AudioClip>();

there is no component called AudioClip. for audio clip get it from prefabs directly to the script or if you want to get it from the audio source that you have in the soundObject, then it will be like :

 clip = soundObject.GetComponent<AudioSource>().clip;

and later you will play it with PlayOneShot which will be waste of time, because you are playing the same clip which is originally taken from this audio source. which actually need just this line to play it

  soundObject.GetComponent<AudioSource>().Play();

finally if you are trying to get the audio clip in the script from the prefabs your code will be like :

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

public class footsteps_script : MonoBehaviour
{

    public GameObject soundObject;
    public GameObject trigger;
    public AudioClip clip;
    public bool nextTrig;

    void Start()
    {
       nextTrig = false;
       //don't forget to assign the your audio clip to the script from the prefabs
    }

    void OnTriggerEnter ()
    {

        if (Bunny_script.nextTrig == true)
        {
            if (clip =! null)
            soundObject.GetComponent<AudioSource>().PlayOneShot(clip);
            nextTrig = true;
            trigger.SetActive(false);
        }
    } 

  }

and if you already have the audio clip in audio source which is attached to the sound object then your code will be :

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

public class footsteps_script : MonoBehaviour
{

    public GameObject soundObject;
    public GameObject trigger;
    public bool nextTrig;

    void Start()
    {
       nextTrig = false;
    }

    void OnTriggerEnter ()
    {

        if (Bunny_script.nextTrig == true)
        {
            soundObject.GetComponent<AudioSource>().Play();
            nextTrig = true;
            trigger.SetActive(false);
        }
    } 

  }
Related