- Home /
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
}
}
}
Do you have an AudioListener in the scene? http://docs.unity3d.com/$$anonymous$$anual/class-AudioListener.html. If you don't have one, the sounds will not be reproduced.
$$anonymous$$ost likely you are using a 3D sound and your audio source is too far from the audio listener. Try changing the clip to a 2D sound.
the black paragraph between the code is just notes. It does not affect the code.
Answer by karsnen · Feb 20, 2019 at 01:04 AM
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.

Answer by NeverHopeless · Jun 29, 2015 at 04:32 AM
A couple of tips:
Make sure your code to play sound runs. By using Debug.Log.
Make sure a clip is attached to the audio source at runtime. You can confirm it by comparing it with
null.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.
it will only play the sound if i make it to where i have to push a key
@jhunglow Have you tried 3rd point ? Play a sound regardless of any condition ? It will verify there is no issue with audio and playing this audio.
Well it will play the audio but not at the right moment. The audio is suppose to play when the attack animation is playing but it will play it a few seconds after the death animation which makes no sense to me
@jhunglow, it means there is no problem with sound file, playing file and listner. You have to check your conditions in which sound should actually play.
Answer by superpentil · Jul 03, 2015 at 07:39 AM
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.
gameObject.GetComponent and GetComponent are identical: the docs for GetComponent omit the unnecessary gameObject reference.
Answer by Vice_Versa · Jun 29, 2015 at 12:36 AM
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
Your answer
Follow this Question
Related Questions
Change Editor Window Update Rate 0 Answers
Car engine sound 1 Answer
Unity 5.6 iOS GVR audio spatializer incompatible? 0 Answers
detect audio then record and then play 0 Answers
When Audio is played through script it buzzes and glitches 2 Answers