【问题标题】:Unity "Can not play a disabled audio source"Unity“无法播放禁用的音频源”
【发布时间】:2017-01-06 10:15:50
【问题描述】:

每当调用该函数时,我一直在尝试播放效果声音,但是我不断收到通知“无法播放禁用的音频源”。我还注意到,即使我将其关闭,声音也会在清醒状态下播放。这是我的 EffectController 类的样子

//--------------------------------------------------------------------------------------------------------------------
//  <copyright file="EffectController.cs" company="ShinjiGames">
//     Copyright (c) ShinjiGames LLC 2016.  All rights reserved.
//  </copyright>
//--------------------------------------------------------------------------------------------------------------------

namespace Assets.Scripts.Effects
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using GameSettings;
    using UnityEngine;

    /// <summary>
    /// Responsible for audio cues and other add-on effects
    /// </summary>
    public class EffectController : MonoBehaviour
    {
        /// <summary>
        /// The sound that plays whenever the player hits a pickup
        /// </summary>
        public AudioSource PickupChime;

        /// <summary>
        /// The current instance of the effect controller
        /// </summary>
        private static EffectController instance;

        /// <summary>
        /// Get the instance of the effect controller
        /// </summary>
        /// <returns>The instance of the effect controller</returns>
        public static EffectController GetInstance()
        {
            return EffectController.instance;
        }

        /// <summary>
        /// When the player hits a pickup on the given position, plays the chime and add an effect
        /// </summary>
        /// <param name="position">The position of the collision</param>
        public void PickupEffect(Vector3 position)
        {
            this.PickupChime.enabled = true;
            this.PickupChime.PlayOneShot(this.PickupChime.clip);
        }

        /// <summary>
        /// When the players hits a bomb, display the bomb explosion 
        /// </summary>
        /// <param name="position">The origin of the explosion</param>
        public void BombEffect(Vector3 position)
        {
            var explosion = GameObject.Instantiate(PrefabManager.GetIntance().BombExplosionPrefab);
            explosion.GetComponent<BombExplosion>().SetOrigin(position);
        }

        /// <summary>
        /// Initialization for the class
        /// </summary>
        private void Start()
        {
            this.PickupChime.enabled = true;
            this.GetComponent<AudioSource>().enabled = true;

            if (EffectController.instance != null)
            {
                GameObject.Destroy(EffectController.instance.gameObject);
            }

            EffectController.instance = this;
            GameObject.DontDestroyOnLoad(this.gameObject);
        }
    }
}

谁能帮帮我?我慢慢的要疯了

编辑 1:我更新了该类,以便它没有接收 AudioSource,而是拥有自己的 AudioSource 组件,并将剪辑接收到铃声,从而解决了问题。 p>

【问题讨论】:

    标签: c# audio unity3d


    【解决方案1】:

    您可能在检查器中未选中音频源。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-21
      • 2023-03-05
      相关资源
      最近更新 更多