I can't get my sound to work!
I have been working on a minigolf game, and I want to add a sound for when the player hits the ball, but it won't play the sound and an error keeps popping up:
PlayOneShot was called with a null AudioClip.
Here is my SoundManager script:
using UnityEngine;
public class SoundManager : MonoBehaviour {
public static AudioClip golfSwingSound;
static AudioSource audioSrc;
void Start()
{
golfSwingSound = Resources.Load<AudioClip> ("Swing");
audioSrc = GetComponent<AudioSource> ();
}
public static void PlaySound (string clip)
{
switch (clip) {
case "Swing":
audioSrc.PlayOneShot (golfSwingSound);
break;
}
}
}
using UnityEngine;
public class PlayerMovement : MonoBehaviour { void Update { SoundManager.PlaySound ("Swing"); } }
My player movement doesn't look like this, but I think this is all you need. Please let me know if you need the entire player movement script. Also sry I'm new so I don't know how to properly space this stuff.
Answer by theMasky · Oct 06, 2020 at 01:27 AM
Well, first, to use Resources.Load, the object you are refering to must be in the "Resources" folder. Something like this: "Assets/Resources/SoundFX/Swing." Then you load that object like this:
Resources.Load("SoundFX/Swing") as AudioClip;
Did you already check that?
Your answer
Follow this Question
Related Questions
Why won't my second Audio Source play? 0 Answers
oneshot not working 0 Answers
Stopping Audio When Zoomed In/Out 0 Answers
Audio playing at hightspeed and simoultanesly various times! 1 Answer
play Audio Array in java 0 Answers