Making random hit noise when hitting tree

Ok im rewriting my question as the people below didnt quite get what i was asking
what line of code do i need to add into function apply damage to make it play a piece of audio whenever the ray cast hits it and how do i go about setting up an array with 3 pieces of audio the code i was given returns errors

#pragma strict
rigidbody.isKinematic = true;
var chop = 1000;
var FallingSound : AudioClip;
//var isKinematic: bool;
//var treemess : ParticleSystem;
private var ChopStreak1 = 0;
private var ChopStreak2 = 0;
function ApplyDamage (Damage : int)
{
chop -= Damage;
if (chop == 0)
{
Dead();
}
}

function Dead()
{
	AudioSource.PlayClipAtPoint(FallingSound,transform.position);
	yield new WaitForSeconds(2);
	rigidbody.isKinematic = false;
	Destroy (gameObject, 20);
}

Alrighty then, that’s a lot of code…

Try this-

I’m not going to script out everything, as I am in a hurry…

you need to make an array with all the audio clips in it- var treeaudio : AudioClip ;

then choose a random clip out of that- treeaudio[Random.Range(0, treeaudio.length)]

This will get you a random audio clip from the array…

If you have any more problems, just comment, I will put some time in to script it out for you :slight_smile:

Here is a basic function to put on the tree

var treesounds : AudioClip [];

function Treefall () {
	audio.Play(treesounds[Random.Range(0, treesounds.length)]);
}

So, when your raycast hits the tree, use GetComponent to call this function!!!

I think you need to do something like…

var myAudioSource : AudioSource;

myAudioSource = this.GetComponent(AudioSource); // I assume script is attached to the object with the audio source. Otherwise you need to create a gameObject Var and find the Object with the AudioSource on it and replace this with that object.

myAudioSource.PlayClipAtPoint(FallingSound,transform.position); //or if it is a 2D sound then you can just myAudioSource.Play();

If you want to do the different sounds there are several ways to do that. One is to create separate empty game objects in the Hierarchy each with a AudioSource on it (audioSourceGameObject1,2,3 etc.)

audioSourceGameObject1

audioSourceGameObject2

audioSourceGameObject3

and connect the respective clip to the AudioSource in the inspector.

Then in the script create an AudioGameObject1 (2, 3 etc.) var for each “audioSourceGameObject1, 2, 3 etc.”

var AudioGameObject1 : GameObject;

var AudioGameObject2 : GameObject;

var AudioGameObject3 : GameObject;

and connect the respective AudioGameObject1, 2, 3 with the var in the inspector. or use an array of AudioGameObject if your into doing things that way.

Then in the code when you need the specific clip do…

myAudioSource = AudioGameObject1.GetComponent(AudioSource);

OR

myAudioSource = AudioGameObject2.GetComponent(AudioSource);

OR

myAudioSource = AudioGameObject3.GetComponent(AudioSource);

Then…
myAudioSource.Play(); // this will play whatever clip is in myAudioSource.

This should work. Hope is all makes sense.

ok after 2 days of fiddling around and getting anoyed i just left the random sounds and got audio playing

#pragma strict
rigidbody.isKinematic = true;
var chop = 1000;
var FallingSound : AudioClip;
//var isKinematic: bool;
//var treemess : ParticleSystem;
private var ChopStreak1 = 0;
private var ChopStreak2 = 0;
var treesounds : AudioClip ;
function ApplyDamage (Damage : int)
{
AudioSource.PlayClipAtPoint(treesounds,transform.position);
chop -= Damage;
if (chop == 0)
{
Dead();
}
}

function Dead()
{
	AudioSource.PlayClipAtPoint(FallingSound,transform.position);
	yield new WaitForSeconds(2);
	rigidbody.isKinematic = false;
	Destroy (gameObject, 20);
}