C# 如何在Unity上使用PlayOneShot()?

C# 如何在Unity上使用PlayOneShot()?,c#,unity3d,C#,Unity3d,我一直在尝试使用此功能在输入触发器时触发声音,但我无法使其工作。我已经读了又读了我的代码,没有任何东西看起来不对劲,看起来和文档上说的一模一样,所以我无法找出我的错误 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class footsteps_script : MonoBehaviour { public Game

我一直在尝试使用此功能在输入触发器时触发声音,但我无法使其工作。我已经读了又读了我的代码,没有任何东西看起来不对劲,看起来和文档上说的一模一样,所以我无法找出我的错误

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);
        }
    }



  

    
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共类足迹_脚本:MonoBehavior
{
公共游戏对象soundObject;
公共游戏对象触发器;
私人音频剪辑;
公共图书馆;
void Start()
{
nextTrig=false;
clip=soundObject.GetComponent();
}
无效OnTiggerEnter()
{
if(Bunny_script.nextTrig==true)
{
soundObject.GetComponent().PlayOneShot(剪辑);
nextTrig=true;
触发器.SetActive(false);
}
}
}

音频源与另一个对象关联。触发器应该在另一个事件发生后播放声音。触发器部分工作正常,因为nextTrig设置为
true
与预期类似,但声音不会播放。此外,声音本身也工作得很好,音量也很好。

它不工作是因为,这是什么

 clip = soundObject.GetComponent<AudioClip>();
clip=soundObject.GetComponent();
没有称为AudioClip的组件。 对于音频剪辑,将其从预置直接获取到脚本中,或者如果您希望从soundObject中的音频源获取,则类似于:

 clip = soundObject.GetComponent<AudioSource>().clip;
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);
        }
    } 

  }
clip=soundObject.GetComponent().clip;
稍后您将使用PlayOneShot播放它,这将是浪费时间,因为您正在播放最初从该音频源获取的相同剪辑。实际上只需要这句台词就可以了

  soundObject.GetComponent<AudioSource>().Play();
soundObject.GetComponent().Play();
最后,如果您试图从预设中获取脚本中的音频片段,您的代码如下:

 clip = soundObject.GetComponent<AudioSource>().clip;
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);
        }
    } 

  }
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共类足迹_脚本:MonoBehavior
{
公共游戏对象soundObject;
公共游戏对象触发器;
公共音频剪辑;
公共图书馆;
void Start()
{
nextTrig=false;
//别忘了把你的音频剪辑分配给预置的脚本
}
无效OnTiggerEnter()
{
if(Bunny_script.nextTrig==true)
{
if(clip=!null)
soundObject.GetComponent().PlayOneShot(剪辑);
nextTrig=true;
触发器.SetActive(false);
}
} 
}
如果您已经将音频剪辑音频源连接到声音对象,那么您的代码将是:

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);
        }
    } 

  }
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共类足迹_脚本:MonoBehavior
{
公共游戏对象soundObject;
公共游戏对象触发器;
公共图书馆;
void Start()
{
nextTrig=false;
}
无效OnTiggerEnter()
{
if(Bunny_script.nextTrig==true)
{
soundObject.GetComponent().Play();
nextTrig=true;
触发器.SetActive(false);
}
} 
}

你确定Bunny_script.nextTrig是真的吗。您可以添加一个Console.Log(“Test”);在if语句之前测试碰撞是否真的发生。是的,我确定。事实上,
nextTrig
确实变为true,这意味着它确实输入了if语句。您是否尝试添加第二个,
volumeScale
参数?请尝试
PlayOneShot(clip,1f)
。非常感谢。我看到了
GetComponent()