Managed to get it fixed, how… no idea.
3 Answers
3In the Unity documentation, the example scripts usually have a dropdown list where it says (f.ex) Javascript, where you can switch to C# or Boo.
http://unity3d.com/support/documentation/ScriptReference/AudioSource.Play.html
Should be no exception.
First thing: to which object this script is attached? If this object is too far from the camera, the sound may be heard too low, or not be heard at all. You must also attach an AudioSource to this object and set its Clip property to the desired sound, or you will get Null Reference errors (pay attention to the Console!).
An alternative to avoid all this setup is to use PlayClipAtPoint:
public AudioClip errorSound; // drag the desired sound here
void OnLeftClickUp(SimonLightPlate.eType color)
{
...
if (!VerifySequence())
{
AudioSource.PlayClipAtPoint(errorSound, Camera.main.transform.position);
ResetGame();
}
...
This will instantiate a temporary AudioSource and play the sound at the camera position (where the game “ears” usually are), destroying automatically the AudioSource when the sound finishes.
From the docs, I think you should do the following: errorSound = Instantiate(Resources.Load("wrong", typeof(AudioClip))); Additionally, the sound should be stored in the Resources folder.
– aldonalettoMy bad! I copied part of the instruction from the docs, and made a big mess. It should be: errorSound = Resources.Load("wrong", typeof(AudioClip));
– aldonalettoI am now trying it with audio.clip = (AudioClip)Resources.Load("wrong"); however now I get an error that I have not attached an audio source to my object, but I have!
Could someone help me with this problem?
public AudioClip wrong;
void OnLeftClickUp(SimonLightPlate.eType color)
{
if (currentState == eState.WAITING_FOR_USER)
{
lightPlates[(int)color].plate.renderer.enabled = false;
if (!VerifySequence())
{
audio.clip = (AudioClip)Resources.Load("wrong");
ResetGame();
}
else
{
if (clickedSequence.Count == sequenceCount)
{
currentState = eState.THINKING;
clickedSequence.Clear();
}
}
}
}
I wish you didn't delete the question so we could have reference to your issue.
– Klown