- Home /
Playing audio from a GUI only app
Hi all,
Can anyone provide an example on how I can play a sound while pressing a GUI button? The solution I've been trying to implement (unsuccessfully) seems overly complex, is that really the way it needs to be done?
I have a sound script which implements a Sound class, which is attached to the main camera, that's where the sounds get assigned to the variables (gosh... how nice it would have been to be able to assign the sound resources in code rather than a drag/drop operation!).
Anyhow, when I try to play one of the sounds by calling this foreign class I keep getting null exceptions (of course, because the sound class has not been instantiated yet) and when I try to instantiate it, I get this error message: You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
Here's my sound class:
using UnityEngine;
using System.Collections;
public class TOADSound : MonoBehaviour {
public AudioClip starttrack;
public AudioClip stoptrack;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void PlayStartTrack() {
audio.PlayOneShot(starttrack);
}
}
I then try to instantiate and call: TOADSound mySound = new TOADSound(); mySound.PlayStartTrack();
This then tanks... any ideas?
Your answer
Follow this Question
Related Questions
Play a multiple audio sources sequentially 2 Answers
www.getAudioClip() Bug: One Second being cut off MP3. 2 Answers
Audio Clip trouble 2 Answers
detect audio then record and then play 0 Answers
When Audio is played through script it buzzes and glitches 2 Answers