- Home /
Triggering a specific sound in an Array - syntax help likely...
hey folks, another newbie question, likely from a JavaScript sytax angle.
i want to send a message to select a specific loop number and have the receiving Game Object trigger that sound from an array. Heres what i have so far:
var targetObject : GameObject;
var soundNum : int;
function OnTriggerEnter()
{
targetObject.SendMessage(targetObject, soundNum );
}
i believe this sets a Game Object to look for and should send an int variable called numSound to the game object
on the receiving end is an object with an AudioSource on it and this script:
var clipNumber : AudioClip[];
function PlayNumberSound(soundNum : int)
{
audio.clip = clipNumber[soundNum - 1]; // i-1 because array starts at 0
audio.loop = true;
audio.play();
}
this sets up an array of sounds which i would like to pass the value soundNum to from the sending script. the following compiles fine but results in a rather ominous MissingMethodException: Method not found: 'UnityEngine.AudioSource.play'. also the sound does not trigger. when the trigger is reached the loop cuts out but no other loop plays.
i'm suspecting a syntax error in terms of my javascript and have tried variations but no luck so far.
any help vastly appreciated,
scott
Funny that this compiles. I don't use JS much, so I'm not sure what works and what doesn't- but I really wouldn't expect this to.
Answer by syclamoth · Feb 19, 2012 at 01:10 AM
Spelling, in programming, is very important. In this case, you need to capitalise the correct words-
audio.play();
isn't a recognised function.
audio.Play();
however, is.
As for your 'index - 1' thing, seriously, just learn to 0-index things. It's not that much of a logical stretch, and it'll make your code more readable.
thanks for the help! i got the 'index -1' thing as part of some starting code i copied from somewhere else. anyway i figured it was something stupid like not capitalizing.