AudioSource not playing audio

For some reason the audio will not play even though it is being called upon in the code.
Here is the code:

using UnityEngine;

using System.Collections;

//Update, the script is now attached to a zombie model animation code is added

public class enemy : MonoBehaviour {

private GameObject wayPoint;

//public float Cube;

private Vector3 wayPointPos;

public Transform zombie_dist;

//This will be the zombie's speed. Adjust as necessary.

public float timer;

private float speed = 3.0f;

public Rigidbody rb;

public GameObject zombie;
Animation Death;
public float dist;
//public CapsuleCollider cc;
public Color newColor;
public bool isClose;
public Transform target;
public float hitBox;
public bool isDead;
public bool isIdle;
public float deathTime; // how long the zombie lives after its first initial death;

//public float attack;


public  AudioSource attack_audio;
void Start ()
{
	attack_audio = GetComponent<AudioSource> ();
	//Attack =  attack_audio.GetComponent<AudioSource>();
	isIdle = false;
	deathTime = 5;
	isDead = false;
	isClose = false;
	Death = zombie.GetComponent<Animation> ();
	timer = 10;
	//cc = GetComponent<CapsuleCollider>();
	rb = GetComponent<Rigidbody>();
	//At the start of the game, the zombies will find the gameobject called wayPoint.
	wayPoint = GameObject.Find("wayPoint");

	//move = GetComponent.<Animation>();

}


void Update ()
{
	if(target != null)
	{
		transform.LookAt(target);
	}
	if (zombie_dist) {
			 dist = Vector3.Distance(zombie_dist.position, transform.position);
			print("Distance to other: " + dist);

	}
if (dist <= 1.5 && isClose == false) {
		speed = 0;
		Death.Play("Attacking");
		attack_audio.Play();

	}

	if (dist >= 8 && isClose == false) {
		speed = 0; 
		Death.Play("Idle");
	}
	if (dist >= 1.6 && isClose == false && dist <= 7) {
		speed = 3;
		Death.Play("Walking");
	}

	if (isClose == true) {
		timer -= 1 * Time.deltaTime;
		speed = 0;
	}
	if (timer <= 0) {
		Destroy(gameObject);
	}

	wayPointPos = new Vector3(wayPoint.transform.position.x, transform.position.y, wayPoint.transform.position.z);
	//Here, the zombie's will follow the waypoint.
	transform.position = Vector3.MoveTowards(transform.position, wayPointPos, speed * Time.deltaTime);
}

//void OnTriggerEnter (Collider other)
//{
//	if (other.gameObject.tag == "bullet") {
//		gameObject.GetComponent<Renderer>().material.color = Color.blue;
//		rb.isKinematic = false;
//		speed = 0;

// cc.isTrigger = false;
// newColor = Color.black;
// }
// }
void OnCollisionEnter (Collision col)//if there is a collision with the prefab projectile the the color of the enemy will be blue (M4A1 gun model compadible)
{
if (col.gameObject.name == “projectile(Clone)”) {
gameObject.GetComponent().material.color = Color.blue;
//rb.isKinematic = false;
speed = 0;
newColor = Color.black;
Death.Play (“Dying”);
isClose = true;
gameObject.GetComponent().enabled = false;

	}
	else if (col.gameObject.name == "bullet_2(Clone)") {
		gameObject.GetComponent<Renderer>().material.color = Color.red;
		//rb.isKinematic = false;
		speed = 0;
		newColor = Color.black;
		Death.Play ("Dying");
		isClose = true;
		gameObject.GetComponent<BoxCollider>().enabled = false;
	
		//if the ak-47 model is enabled the the gun will shoot the prefab bullet_2 which make the enemy color red
	}

}

}

I had an issue where the audio was not playing. My code, project settings were set A right but for the game scene panel within the Unity Editor. Make sure “Mute Audio” is disabled in that panel.

just in case for anyone facing similar issues.

A couple of tips:

  1. Make sure your code to play sound runs. By using Debug.Log.
  2. Make sure a clip is attached to the audio source at runtime. You can confirm it by comparing it with null.
  3. For testing purpose, write a routine that plays a sound regardless of any condition and repeats it after some delay.

Using these tips, i hope you can troubleshoot.

Just in case anyone else stumbles upon this and nothing else here steers you in the right direction, check the pitch on your audio source. I accidentally set mine to 0 and it caused no audio to play.

this might be a bit dated(did t his in unity 4) but try this

public AudioClip yourSound;

public void PlaySound()
{
  audio.clip = yourSound;
  audio.Play();
}

then in another method you would just call PlaySound. my code didnt use GetComponentAudioSource() but the game object using it had an audio source component attached to it.
but again, not sure if playing sounds in unity still works this way

Since this requires an AudioSource I’d put a RequireComponent line after the Library stuff is called at the beginning of your script. In C# this is done with [RequireComponent(typeof(AudioSource))]. This is just to make sure.

As for your main stuff, try:

attack_audio = gameObject.GetComponent<AudioSource>();

in Start() instead of

attack_audio = GetComponent<AudioSource>();

The difference is that gameObject references the current instance which is what you want.