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
0
Question by miketyler1 · Dec 16, 2013 at 02:19 AM · gameobjectguitextaudio sourceaudioplayaudio.play

audio.Play() not working?

Alright, so I have the following script called Timer.cs.

 Using UnityEngine;
 using System.Collections;
 
 public class Timer : MonoBehaviour {
     
 
     public float timer = 300; // set duration time in seconds in the Inspector
     public static int sound = 1;
     public static int go = 1;
     bool  isFinishedLevel = false; // while this is false, timer counts down
 
     void  Start(){
         PlayerController.speed = 8;
         PlayerController.jumpHeight = 12;
     }
     
     void  Update (){
         if (!isFinishedLevel) // has the level been completed
         {
             timer -= Time.deltaTime; // I need timer which from a particular time goes to zero
         } 
         
         if (timer > 0)
         {
             guiText.text = timer.ToString();
         } 
         else 
         {
             guiText.text = "GAME OVER!"; // when it goes to the end-0,game ends (shows  time text   over...) 
 
             audio.Play();
 
             int getspeed = PlayerController.speed;
             PlayerController.speed = 0;
 
             int getjumpHeight =  PlayerController.jumpHeight;
             PlayerController.jumpHeight = 0;
         }
         if (Input.GetKeyDown("r")) // And then i can restart game: pressing restart.
         { 
             Application.LoadLevel(Application.loadedLevel); // reload the same level
         }
     }
 }

This script is connected to some GUI Text, which displays the amount of time remaining in the game. Also attached to this script is an Audio Source with my desired sound selected. When the clock reaches zero, the text changes to say "GAME OVER!" and the character controls lock up; however, the sound does not play. All other instances of audio.Play() in my scene are working fine, and when I set the Audio Source to "Play On Awake", it plays without a problem. What could be the problem?

Thanks in advance.

Comment
Add comment · Show 2
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 iwaldrop · Dec 16, 2013 at 02:39 AM 1
Share

Where is the audio source located in the scene, and is it set to 2d or 3d sound? $$anonymous$$ight it be playing but be too quiet to hear? Otherwise I'd expect some kind of error. You can also check to see if audio is null with a Debug Log statement.

avatar image mahdiswe · Jan 05, 2017 at 06:04 PM 0
Share

Same here .. audio.Play() // Did not work While AudioSource.PlayClipAtPoint(clip,transform.position) // Worked fine, I don't know why

5 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by Hyperion · Dec 16, 2013 at 02:36 AM

Try changing audio.Play() into AudioSource.PlayClipAtPoint(clip,transform.position) and tell me the result. And remember to assign an AudioClip variable prior to that. Here's a link on it if you need it: http://docs.unity3d.com/Documentation/ScriptReference/AudioSource.PlayClipAtPoint.html

This method has never failed me, so I hope it won't fail you either.

Comment
Add comment · Show 3 · 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 Chino_96 · Aug 30, 2017 at 02:12 PM 0
Share

Thanks I use the audioclip variable and it works one time, so when i play it just sound one time and it didnt even collide with something, and is not check play on awake, i think is the colliders o my script. I would be glad if you can help me.
This is my script:

u

sing UnityEngine;

using System.Collections; public class Sonido : $$anonymous$$onoBehaviour { public AudioClip clip; public AudioSource source; public void Awake() { AudioSource.PlayClipAtPoint(clip, transform.position); } public void OnTriggerEnter(Collider other) { if (gameObject.tag == "Punto") { AudioSource.PlayClipAtPoint(clip, transform.position); } }<
avatar image Karaflakos21 · Nov 08, 2017 at 04:32 PM 0
Share

Using unity 2017.2 I had a problem while playing audio, using this it worked, can you explain why?

avatar image Jeff_Nasc · Jun 27, 2021 at 07:06 PM 0
Share

Esse método resolveu pra mim também. Na unity o audio funcionava bem, no apk não. Obrigado

avatar image
0

Answer by BlastOffProductions · Feb 15, 2017 at 07:37 AM

This is what I use: Debug.Log("Hey2"); GetComponent<AudioSource>().Play();

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 bonez9oh5 · Dec 18, 2017 at 02:51 AM 0
Share

Wouldn't that only wok if there is a single audiosource in scene?

avatar image
0

Answer by busybyte · Jun 30, 2019 at 03:28 PM

Pretty late, but i had the same problem today and the solution is, not to start play in short loops like update. Check if audio isplaying is false, otherwise you start the clip, every time you call play, from the beginning and you do not hear anything.

Comment
Add comment · 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
0

Answer by Fenikkel · Oct 19, 2020 at 09:36 AM

In my case, the sound plays on awake, but then don't work when y call AudioSource.Play()

My problem was that I run out of memory or something like that. All because a use too much VideoPlayer.Prepare

Check if you are loading too much resources.

Comment
Add comment · 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
0

Answer by Devster2020 · Jun 28, 2021 at 03:47 PM

Hi @miketyler1 the problem is that your Update method restart every frame, so when the timer reach 0 for the first time, it remains to 0 for each other frame.. in this way, every frame execute this code:

  guiText.text = "GAME OVER!"; // when it goes to the end-0,game ends (shows  time text   over...)
 audio.Play();
 
 int getspeed = PlayerController.speed;
 PlayerController.speed = 0;
 
 int getjumpHeight =  PlayerController.jumpHeight;
 PlayerController.jumpHeight = 0;

So, your AudioController will restart playing the sound continuously, making it inaudible. To resolve this, add this code at the start of Update method:

 if(timer <= 0)
     return;

or avoid with other methods to execute continuously that piece of code.

Comment
Add comment · 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

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

27 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

No position variable for GUIText()? 2 Answers

Multiple Targets 1 Answer

How to play music/audio 2 Answers

GetComponent, int error, if statement, problem. 1 Answer

how to make fixed guiText ? 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