I jumbled a few Tutorials together for my first simple game.
I have 2 audio sources under my GameObject that I use as the character. They both play via "play on awake". Only one plays from script. The other either doesn’t sound at all or gives a few clicks when called from script.
I am using the most current Visual Studio to write Scripts
Here's a portion of my script that isn't working correctly (I have left out a lot of player movement script).
audio variable on line 7, audio play on line 33
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public Rigidbody rb;
public AudioSource AudioFall;
public GameObject Fall;
if(rb.position.y < -1f)
{
AudioFall.Play();
Fall.SetActive(true);
FindObjectOfType<GameManager>().GameOver();
}
Here is a portion in a separate .cs that is working
audio variable on line 7, audio play on line 17
using UnityEngine;
public class playerCollision : MonoBehaviour
{
public PlayerMovement movement;
public AudioSource AudioCrash;
public GameObject Crash;
void OnCollisionEnter(Collision collisionInfo)
{
if (collisionInfo.collider.tag == "Obstacle")
{
FindObjectOfType<PlayerMovement>().rb.isKinematic = true;
movement.enabled = false;
AudioCrash.Play();
Crash.SetActive(true);
FindObjectOfType<GameManager>().GameOver();
}
}
I have tried to increase max distance in the audiosource inspector.
You should check these three things:
1.It looks like your audio is being played each frame while if(rb.position.y < -1f)
is true
since the Debug.Log
is called multiple times. Simply check if it is playing before playing it:
if(rb.position.y < -1f)
{
//PLAY ONLY IF AUDIO IS NOT PLAYING
if (!AudioFall.isPlaying)
{
AudioFall.Play();
}
Fall.SetActive(true);
FindObjectOfType<GameManager>().GameOver();
}
2.If that's not the case then please verify that audio clip is assigned to the clip slot of the AudioFall Audiosource
in the Editor.
3.Finally, if the Fall
GameObject is the-same as the GameObject the audio is attached to then the audio won't play because it being set to inactive. Check if it is being set in-active anywhere. You have to attach the AudioFall Audiosource
to another empty GameObject that will not be deactivated.