c# play music on collision not working. why?

Good day!

I would like to play a music when my runner collides with another game object “food”.
The music is blips. I created " public AudioClip blips;" in my code and added
" AudioSource.PlayClipAtPoint(blips[0],transform.position);" after the “void OnCollisionEnter2D (Collision2D other) {”. The problem is, when I played my game, the music was not played as expected. May I know what went wrong with the code?

here is the entire code for you reference. I appreciate your response!

using UnityEngine;
using System.Collections;

public class runner : MonoBehaviour {  
	
	public Vector2 jumpForce = new Vector2(0, 1);

	public AudioClip[] blips;
	
	void Update () {
		
		if (Input.GetKeyUp("space")) {          
			rigidbody2D.velocity = Vector2.zero;            
			rigidbody2D.AddForce(jumpForce);            
		}
		
		Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
		
		if (screenPosition.y > Screen.height || screenPosition.y < 0)      
			Die();
	}
	
	void OnCollisionEnter2D (Collision2D other) {

		
		if (other.gameObject.tag == "food")
			AudioSource.PlayClipAtPoint(blips[0],transform.position);
			Destroy (other.gameObject);


		else
			Die();
	
	}
	
	void Die () {
		Destroy(this.gameObject);
		Application.LoadLevel(Application.loadedLevel);
	}
}

First, add a Debug.Log(“Collision Enter”) on the first line of the void OnCollisionEnter2D (Collision2D other) function.
This way we know if the collider is working OK or not. I suspect that your problem is that the object you want to collide with , in this case the food item, does not have a collider2d, or that your player object does not have a rigidbody.
Keep in mind that 2D and 3D physics don’t interact, so also make sure that the collider in your food item is also a 2D collider and not a regular one.

Hello,

Maybe try this :

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(AudioSource))]
public class runner : MonoBehaviour {  
 
    public Vector2 jumpForce = new Vector2(0, 1);
 
    public AudioClip[] blips;
 
    void Update () {
 
        if (Input.GetKeyUp("space")) {          
            rigidbody2D.velocity = Vector2.zero;            
            rigidbody2D.AddForce(jumpForce);            
        }
 
        Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
 
        if (screenPosition.y > Screen.height || screenPosition.y < 0)      
            Die();
    }
 
    void OnCollisionEnter2D (Collision2D other) {
 
 
        if (other.gameObject.tag == "food")
        {
            audio.PlayOneShot(blips[0]); //part that changed
            Destroy (other.gameObject);
        }
        else
            Die();
 
    }
 
    void Die () {
        Destroy(this.gameObject);
        Application.LoadLevel(Application.loadedLevel);
    }
}

To play a sound you need an audio Source on your runner