2

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.

Amit Kumar Singh
  • 4,393
  • 2
  • 9
  • 22
  • You need to put `Debug.Log` inside `if(rb.position.y < -1f)` to see if that condition is even true and also to see how many times it is called. – Programmer Jun 27 '17 at 00:32
  • @Programmer Thanks! so i tried this if(rb.position.y < -1f) { Debug.Log(AudioFall.time); AudioFall.Play(); Fall.SetActive(true); FindObjectOfType().GameOver(); } – Dustin Gilding Jun 27 '17 at 01:08
  • The result was this after i ran the Game Object off the the edge was five notes in the console [ UnityEngine.Debug:Log(Object) 0, UnityEngine.Debug:Log(Object) 0.021, UnityEngine.Debug:Log(Object) 0, UnityEngine.Debug:Log(Object) 0, UnityEngine.Debug:Log(Object) 0 ] – Dustin Gilding Jun 27 '17 at 01:12

1 Answers1

1

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.

Programmer
  • 121,791
  • 22
  • 236
  • 328