mavericks
mavericks

Reputation: 115

Unity sound when cube hit wall

I have sound source and my question is how can I play that sound when my cube hit a wall?

I tried something like this, but it doesn't work.

usingUnityEngine;
usingSystem.Collections;

public class sound : MonoBehaviour {

voidOnTriggerEnter (Colliderother) 
{
if(other.gameObject.tag == "wall")
{

Audio.PlayOneShot(sound);

}

}
}

I hope that someone will answer me. It's too important for me. Last few days I searched the whole Internet, I did so many changes on my game, but nothing work :(

Kind regards

Upvotes: 0

Views: 218

Answers (1)

Gurjit Singh
Gurjit Singh

Reputation: 1120

usingUnityEngine;
usingSystem.Collections;

public class sound : MonoBehaviour {

public AudioSource soundEffect;

voidOnTriggerEnter (Collider other) 
{
   if(other.gameObject.tag == "wall")
   {
      soundEffect.Play ();
   }
}
}

Assign audio source to script in inspector and assign script to cube.

Upvotes: 1

Related Questions