【问题标题】:AudioSource doesn't work very well with particle SystemAudioSource 不适用于粒子系统
【发布时间】:2017-01-26 14:40:39
【问题描述】:

好吧,我的问题是指粒子系统和声音,我不是 100% 确定它与粒子系统这一事实有关,基本上我的游戏着火了,当我的球与fire 它应该播放声音,问题是它找不到 AudioSource 并一直说没有附加 AUdioSource,但我已经使用 2 种不同的方式做到了,通过检查器附加 audioSource,并将其设置为私有并获取gameObject 中 audioSource 的组件,那我做错了什么? :S

代码如下:

using UnityEngine;
using System.Collections;

public class particlCol : MonoBehaviour {

    Manager gameManager;
    public AudioClip aud;
    private AudioSource audioC;

    void Start()
    {
        audioC = GetComponent<AudioSource> ();
        gameManager = GameObject.Find ("GameController").GetComponent<Manager> ();
    }

    void OnParticleCollision(GameObject other)
    {

        if (other.tag == "Player") {
            Debug.Log ("entrou");
            audioC.clip = aud;
            audioC.Play ();
            gameManager.LifeDown ();
        }
    }
}

对象的屏幕截图:

Ps:之前它工作得很好,当它碰到火时,我在游戏中失去了生命,所以问题不在于结肠本身

【问题讨论】:

  • 您的 AudioSource 作为组件位于何处?哪个对象?您能否向我们展示层次结构和所涉及对象的属性的屏幕截图?
  • 它附加到同一个游戏对象,我在粒子游戏对象上有脚本,但我放了 2 分钟的屏幕截图

标签: c# unity3d unity5


【解决方案1】:

你可以做一件事:

创建一个gameObj,将你想播放的音频带到hierarchy,取消选中play on awake属性,然后将其设为上述gameObject的子级。

然后创建一个脚本并在该脚本中创建 AudioSource 变量并在 Start() 中执行以下操作: //示例

公开 AudioSource 拍摄;

开始(){

shoot=GetComponent();

}

//就是这样

现在将此脚本附加到上面制作的 GameObject 并将作为子项的声音文件附加到脚本 AudioSource 变量选项(否则将不显示)。

现在无论你想在哪里播放那个声音,你只需要调用那个脚本(

【讨论】:

    【解决方案2】:

    签入 Awake 方法是您的 AudioSource null 是这样的:

      void Awake()
     {
         Debug.Log("AudioSource: " + GetComponent<AudioSource>());
     }
    

    或者试试:

    [RequireComponent(typeof(AudioSource))]
    

    我读到该错误可能是由音频剪辑引起的,因此请尝试重新导入您的声音。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-04
      • 1970-01-01
      • 2011-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多