x


Play audio C# [fixed]

Managed to get it fixed, how.. no idea.

more ▼

asked Jun 10 '12 at 10:13 PM

Suraci gravatar image

Suraci
46 7 9 10

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

In 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.

more ▼

answered Jun 10 '12 at 10:58 PM

Wisearn gravatar image

Wisearn
116 2 3 3

Ohh gawd, I feel so stupid right now. I completely overlooked that part :|. Thanks

Jun 10 '12 at 11:02 PM Suraci
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jun 10 '12 at 11:00 PM

aldonaletto gravatar image

aldonaletto
41.4k 16 42 197

I am having some problems with loading in my sound, I am trying to use

errorSound = (AudioClip)Resources.Load("Assets/wrong.wav");

but it's not working nor am I sure if this is the proper way to do so, what am I doing wrong if I may ask?

(I've attached my sound file to the object that needs it, and when I run the program without the extra errorSound it's working but without sound obviously.)

Jun 11 '12 at 11:53 AM Suraci

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.

Jun 11 '12 at 02:05 PM aldonaletto

Getting 4 errors, '=' and ')' invalid token, method must have a return type and identifier expected.

I'm completely lost on this part now.

Jun 11 '12 at 03:22 PM Suraci

My bad! I copied part of the instruction from the docs, and made a big mess. It should be:

errorSound = Resources.Load("wrong", typeof(AudioClip));
Jun 11 '12 at 04:58 PM aldonaletto

When I use errorSound = Resources.Load("wrong", typeof(AudioClip)); I still get some errors about invalid tokens '(' ')' '='.

But when I run it as

public AudioClip errorSound = Resources.Load("wrong", typeof(AudioClip)); I get

"Cannot implicitly convert type unityEngine.Object to UnityEngine.AudioClip. An explicit conversion exists ( are you missing a cast?)"

Any idea on what's going wrong?

(going to give "audio.clip = (AudioClip)Resources.Load("wrong.wav");" a shot, hopefully this will do the trick).

Jun 11 '12 at 05:32 PM Suraci
(comments are locked)
10|3000 characters needed characters left

I 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();
          }
         }
       }

    }
more ▼

answered Jun 11 '12 at 05:31 PM

Suraci gravatar image

Suraci
46 7 9 10

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4146
x1026

asked: Jun 10 '12 at 10:13 PM

Seen: 3806 times

Last Updated: Jun 11 '12 at 09:57 PM