- Home /
AudioClip doesn't play sound

Hello, again. I'm not a native English speaker, so please be generous with my grammatical faults.
I want to play two audio clip as background music.
The one(names 'audioNormal') has to play as game start, the other(names 'audioFaster') has to play when time remains less than half.
"audioNormal" clip is attached to Main Camera.
I thought "audioFaster" clip should be also atteched to Main Camera, too.
However, it never works.
Here's my script and it is also attached Main Camera.
(The Image I attached is an Inspector window of Main Camera.)
#pragma strict
@script RequireComponent(AudioSource)
var audioNormal:AudioClip;
var audioFaster:AudioClip;
var time: GameObject; // need to get time infomation
function Start ()
{
audio.clip = audioNormal;
audio.Play();
time = gameObject.Find("GUIs/txtTime");
Debug.Log("1");
}
function Update ()
{
if(time.GetComponent(TimeGUI).startTime-Time.time < 31.0)
{
Debug.Log("2");
audio.Stop();
audio.clip = audioFaster;
Debug.Log("3");
audio.Play();
}
}
I used Debug.Log script to check the state of communication between two other scripts, and all of them have printed well.
Of course audio files doesn't have any problem.
Now I am so confused.
What did I wrong? It's too hard to find by myself.
Please help me.
No, only second audio clip('audioFaster' in my script) doesn't play sound. 'audioNormal' plays well.
Answer by emc233 · Dec 25, 2013 at 10:33 PM
you cant say: audio.clip = audioNormal;
instead you should say: this.gameObject.audio.clip = audioNormal; this.gameObject.audio.Play();
Oh..I'm afraid to say, but it doesn't work. The reason I wrote that way, because it is in the Unity Script Reference. Though, thank you for the answer.
This answer is not really doing anything else than the code that @fancytank already have written. this.gameObject.audio vs audio achieves nothing else than perhaps makes the code read out a bit more explicit about your intentions. It's just a more verbose way of asking for the same thing.
On second thought, It looks like you want to background music for your level. You should make it a 2-d sound. I am not sure if that will help, but it is just a thought. You can also check off play on awake...
Your answer
Follow this Question
Related Questions
Audio not playing 1 Answer
2nd Audio Clip Not Playing 0 Answers
How to make audio not playing repeatedly? 1 Answer
Best way to change looping sound 2 Answers
How can I play a sound effect many times without cutting the last sound effect? 1 Answer