- Home /
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.
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.
Same here .. audio.Play() // Did not work While AudioSource.PlayClipAtPoint(clip,transform.position) // Worked fine, I don't know why
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.
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:
uusing 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); } }<sing UnityEngine;
Using unity 2017.2 I had a problem while playing audio, using this it worked, can you explain why?
Esse método resolveu pra mim também. Na unity o audio funcionava bem, no apk não. Obrigado
Answer by BlastOffProductions · Feb 15, 2017 at 07:37 AM
This is what I use: Debug.Log("Hey2"); GetComponent<AudioSource>().Play();
Wouldn't that only wok if there is a single audiosource in scene?
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.
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.
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.
Your answer
Follow this Question
Related Questions
When Audio is played through script it buzzes and glitches 2 Answers
AudioSource.clip.time won't work? 2 Answers
GetComponent, int error, if statement, problem. 1 Answer
multiple audio sources playing 2 Answers