How to call(start play) Sound attached to a gameObject by destroing other gameObject?

I have 2 game objects. Fist - Barrel,Second - Player. After destroying barrel i need to start play Sound attached to Player.

Script on Barrel(BarrelSmasher.js):

function OnMouseDown()
{
Destroy( this.gameObject );
}

Script on Player(HitSound.js):

var HitSound : AudioClip[];

function HitSoundStart()
{
var volume = Mathf.Clamp01(Time.time);
HitSound.PlayOneShot(HitSound[Random.Range(0, HitSound.Length)], volume);
}

Please help =)

You can make your Player a Singleton object (I’m assuming you only need one instance of the player). And then call a public method on Player when the OnDestroy() function gets called from BarrelSmasher. Here is the code.

BarrelSmasher.js

function Start () 
{
	OnMouseDown();
}

function OnMouseDown()
{
	Destroy( this.gameObject );
}

// This function is called when the MonoBehaviour will be destroyed.
function OnDestroy () 
{
	Player.GetInstance().PlayHitSound();
    print("Script was destroyed");
}

HitSound.js

// Changed AudioClip to AudioSouce
// Accept only one reference not an array to make things simple.
var HitSound : AudioSource;
 
// This funtion is called from some one else, who has access to this script.
public function HitSoundStart()
{
	HitSound.volume = 1f;
	HitSound.Play();
}

Player.js

private var  m_HitSound : HitSound;

// This is where the singleton instance gets assigned from. 
function Awake () {
	m_Instance = this;
}

function Start () {
	// Keep a reference from HitSound script
	m_HitSound = GetComponent(typeof(HitSound)) as HitSound;
}

// Public funtion to trigger the sound playing funtionality.
public function PlayHitSound()
{
	if(m_HitSound)
	{
		m_HitSound.HitSoundStart();
	}
}

// Create a Singleton object
// Refer for further info : http://en.wikipedia.org/wiki/Singleton_pattern
private static var m_Instance : Player = null;

public static function GetInstance() : Player
{
	if(m_Instance == null)
	{
		Debug.LogError("Player script is not assiged to a GameObject.");
	}
	
	return m_Instance;
}

I’ve tested this and it works fine.

Hope this would be of help!.