Hi im new in Unity but i was very Long a c# developer but i have a Problem with the Sound. I want that Unity Play a Sound on collision the collision is working but no Sound. using System.Collections; using System.Collections.Generic; using UnityEngine; [RequireComponent(typeof(AudioSource))] public class Collideing : MonoBehaviour { public AudioClip collisionSound; AudioSource audiosource; private void OnCollisionEnter2D(Collision2D collision) { audiosource = GetComponent<AudioSource>(); if (collision.relativeVelocity.y <= 0f); { Rigidbody2D rb = collision.collider.GetComponent<Rigidbody2D>(); if (rb != null) { audiosource.PlayOneShot(collisionSound, 0.7f); Destroy(gameObject); Destroy(this); } } } }
Please use code tags when you are posting code to the forums, it becomes much easier to read. I can see that you're destroying your game object right after playing the clip. This gameobject contains your AudioSource component, so by destroying the gameobject you're also destroying the audio source, as well as "this" component (that second Destroy is redundant). An easy way to delay destruction is to pass a time value in seconds to the Destroy function as a second parameter. Destroy(gameObject, collisionSound.length);