Have solved the problem but don't know enough to describe exactly how I did so.
Can not play a disabled audio source, but the source is definitely not disabled!
This is the error message I'm getting:
Can not play a disabled audio source
UnityEngine.AudioSource:Play()
SoundManager:BallHitsBlockSound() (at Assets/Scripts/SoundManager.cs:20)
Block:OnCollisionEnter2D(Collision2D) (at Assets/Scripts/Block.cs:11)
As you can see in the Inspector screenshot below, the SoundManager object is enabled, and the AudioSource component is enabled. Any idea what could be causing this? Based on another answer elsewhere I have checked to be sure that this script is only on one object.
I don't believe the problem is in code, but just in case I have included the script in question:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SoundManager : MonoBehaviour
{
AudioSource audioSource;
public AudioClip blockHit;
public AudioClip racketHit;
private void Start()
{
print("I'm attached to " + name);
audioSource = GetComponent<AudioSource>();
}
public void BallHitsBlockSound()
{
audioSource.clip = blockHit;
audioSource.Play();
}
public void BallHitsRacket()
{
audioSource.clip = racketHit;
audioSource.Play();
}
}
Thank you for any help you can give! :)
If you remark the lines that change the clip, does the sound play using the default sound?
Thank you for your reply. Apparently the problem is that I was trying to access the function by calling it from an object like Sound$$anonymous$$anager.BallHitsBlockSound();, when actually it needs to be called from a script, so first I had to make it realize I wanted it to use the script. This was hard to diagnose because my object and script were both named Sound$$anonymous$$anager, so in the future I'll probably add "Script" to every script just to make it easier for myself. Anyway, I've sorted it out now even though I don't exactly understand how. Off I go to learn some more. :)
Follow this Question
Related Questions
Play sound when move by using script 0 Answers
BCE0004: Ambiguous reference 'audio': Piano 1.audio, UnityEngine.Component.audio 0 Answers
Having multiple audio sources in a single object? 0 Answers
Looping wav creates gap between plays 0 Answers
AudioSource.Play plays every sound at once,AudioSource.Play Plays every source at once 0 Answers