Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by jhunglow · Jun 26, 2015 at 07:41 AM · audio

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
     }

 }

}

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BiG · Jun 26, 2015 at 07:41 AM 0
Share

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.

avatar image jhunglow · Jun 28, 2015 at 10:17 PM 0
Share

yes i have an audio listener

avatar image maccabbe · Jun 29, 2015 at 12:21 AM 0
Share

$$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.

avatar image jhunglow · Jun 29, 2015 at 12:23 AM 0
Share

the black paragraph between the code is just notes. It does not affect the code.

avatar image jhunglow · Jun 29, 2015 at 12:29 AM 0
Share

I set it to 2D but that did not fix the problem.

Show more comments

4 Replies

· Add your reply
  • Sort: 
avatar image
7

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.

alt text


screen-shot-2019-02-19-at-50313-pm.png (25.2 kB)
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_VMMglh6z1305Eg · Apr 24, 2020 at 05:24 AM 0
Share

Thank you for saving me from my own idiocy!

avatar image crayo7 · 6 days ago 0
Share

You saved my life Thank you

avatar image
1

Answer by NeverHopeless · Jun 29, 2015 at 04:32 AM

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jhunglow · Jun 30, 2015 at 11:38 PM 0
Share

it will only play the sound if i make it to where i have to push a key

avatar image NeverHopeless · Jul 02, 2015 at 04:41 AM 0
Share

@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.

avatar image jhunglow · Jul 03, 2015 at 01:21 AM 0
Share

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

avatar image NeverHopeless · Jul 03, 2015 at 10:51 AM 0
Share

@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.

avatar image
-1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Doches · Sep 06, 2019 at 10:22 AM 0
Share

gameObject.GetComponent and GetComponent are identical: the docs for GetComponent omit the unnecessary gameObject reference.

avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jhunglow · Jun 29, 2015 at 12:47 AM 0
Share

it does not recognize audio in audio.clip

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Realistic Sound Effect 0 Answers

Deleting an AudioClip created by the microphone 0 Answers

Audio stuttering on Android when timescale == 1, but not when 0.01 0 Answers

Audio Mixer - send to different position? 0 Answers

Audio group problem at standalone 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges