DzoniGames
DzoniGames

Reputation: 91

Unity C# - Audio Source not Playing

Recently I started making a prototype game in Unity. But I have some problems with the audio. I use audio.Play(); but nothing happens. Here is the code:

The initialization:

public AudioClip jumpland;
AudioSource audio;
void Start() { audio = gameObject.GetComponent<AudioSource> (); }

Playing the Sound:

void OnTriggerEnter2D(Collider2D col) { 
    if (down.GetComponent<BoxCollider2D> ().gameObject.tag == "block") {

        audio.clip = jumpland;
        audio.Play ();
    }
}

The audio clip is assigned but it wont play.

Upvotes: 0

Views: 1316

Answers (1)

DzoniGames
DzoniGames

Reputation: 91

Ok so I had to play it from the "down" game-object script. And I put it in a method so I made this method:

PlaySound(AudioClip clip) {

audio.clip = clip;
audio.Play();
}

and I just call this by using:

PlaySound(jumpland);

Upvotes: 1

Related Questions