Can someone take a look at my simple code below and give me a little help. I would like to tie the script to a key in the input editor to activate and deactivate my Music track. Where the music is off by default(for this simulation) Here is the code… Any help would be appreciated.
var Music ON/OFF : AudioClip;
function Update ()
{
if (!audio.isPlaying)
{
audio.clip = Music ON/OFF;
audio.Pause();
}
}
Thank you for your time,
Gary Haus
“Music ON/OFF” is not a valid variable name. You can’t have spaces or special characters in a variable name. use MusicOnOrOff instead. (or just plain Music)
Besides that, the code doesn’t seem to have anything to do with the purpose you intend for it…
var playing : boolean = true;
function Update() {
if (Input.GetKeyDown("m")) {
playing = !playing;
}
if (audio.isPlaying() !playing) audio.Stop();
if (!audio.isPlaying() playing) audio.Play();
}
Starmanta thanks. I see the errors in my code. I purchased a couple of books this weekend on programming and I am learning as quickly as possible. It is obviously going to take me some time… That being said. I am receiving the following error from the code you pasted above.
Assets/Standard Assets/Scripts/Music_ON_OFF.js(7) error BCE0077: It is not possible to invoke an expression of type ‘boolean’.
Thanks,
Gary
Shoot. audio.isPlaying needs to not have the () after it.
Hehe! It works great now. Thank you! Perfect.
Gary
Could someone show me how to add the ability to have a key on the joystick as well to enable or disable my music? I have been having a bear of a time trying to do this. It would be nice if I could do this as well.
Cheers,
Gary
var playing : boolean = true;
function Update() {
if (Input.GetKeyDown("p")) {
playing = !playing;
}
if (audio.isPlaying !playing) audio.Stop();
if (!audio.isPlaying playing) audio.Play();
}
var playing: boolean = true;
function Update()
{
if (Input.GetKeyDown("p") || Input.GetKeyDown("Joystick Button 1"))
{
playing = !playing;
}
if (audio.isPlaying !playing) audio.Stop();
if (!audio.isPlaying playing) audio.Play();
}
Something like that? there either the joystick button or the key p can be used.
or:
function Update()
{
if (Input.GetKeyDown("p") || Input.GetKeyDown("Joystick Button 1"))
{
if (audio.isPlaying)
{
audio.Stop();
}
else
{
audio.Play();
}
}
}
…without your ‘playing’ flag.
Thank you. Just make sure Joystick Button 1 or whatever is lowercase.
i.e. joystick button1
Gary
Thanks again. I wouldn’t have figured out the code by myself. No need to apologize. Thanks for your help.
Gary