1
votes

I have been trying to play an effect sound whenever the function is called, however I keep getting the notification "Cannot play a disabled audio source". I also noticed that the sound plays on awake even though I have it turned off. Here's what my EffectController class looks like

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

Can anyone give me a hand? I am slowly going crazy

Edit 1: I updated the class so that instead of taking in an AudioSource, it have it's own component for AudioSource and takes in the clip to the chime instead, which solves the problem.

1

1 Answers

1
votes

You may have had the Audio Source unchecked in the inspector.