2
\$\begingroup\$

I trying to play a sound while the player is ontriggerenter. The sound is linked to the object with a AudioSource component. But notting happened.

using UnityEngine;
using System;
using System.Collections;

public class FloatBehavior : MonoBehaviour {

    Vector2 floatY;
    float originalY;

    public float floatStrength = 1;

    AudioSource triggerAudio;                                    // Reference to the AudioSource component.

    void Start () {
        this.originalY = this.transform.position.y;
        triggerAudio = GetComponent <AudioSource> ();
        Debug.Log (triggerAudio);
    }

    void Update () {
        transform.position = new Vector3(transform.position.x,
                                         originalY + ((float)Math.Sin(Time.time) * floatStrength),
                                         transform.position.z);
    }

    // OnTriggerEnter 2D
    void OnTriggerEnter2D(Collider2D other) {

        // If gameObject comes in contact with player
        if (other.gameObject.tag == "Player") {
            // Play the hurt sound effect.
            triggerAudio.Play ();
        }


    }



}

Trying to debug, but the triggerAudio object will retrieve: woodstump 3 (UnityEngine.AudioSource) UnityEngine.Debug:Log(Object) FloatBehavior:Start() (at Assets/C#/Objects/FloatBehavior.cs:17).

What am I missing in my script? Should I use AudioClip also?

\$\endgroup\$
1
\$\begingroup\$

Well the first step is to verify that your OnTriggerEnter2D function is getting called. It's easy enough to put a debug log in there with who it collided to.

An audio source is just that -- it's a "source" audio. It contains important things like positioning and so forth. On the audio source is a field for audio clip. Assuming you have a clip in there already (e.g. this is on a prefab and you can edit it in the inspector), then calling Play should play it.

Check the audio source to see that it's not firing on startup, that might be another issue.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

check that audio listener is atached at the camera, and the camera is close enough to the audio source position.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

You can put the Audio file into the Resources folder and load it from there.

...

public class FloatBehavior : MonoBehaviour {
    ...

    AudioSource triggerAudio;                 // Reference to the AudioSource component.

    void Start () 
    {
        ...
        triggerAudio= this.gameObject.AddComponent<AudioSource>();
    }

    void Update () 
    {
        ...
    }

    void OnTriggerEnter2D(Collider2D other) 
    { 
            ...                
            AudioClip ac = Resources.Load("myFile") as AudioClip;

            triggerAudio.PlayOneShot(ac, 1f);
    }

}

Hope it helps!

| improve this answer | |
\$\endgroup\$
-1
\$\begingroup\$

gameObject.GetComponent‹AudioSource›().Play();

| improve this answer | |
\$\endgroup\$
  • 2
    \$\begingroup\$ Hi RedDragon, can you please expand your answer? It's too short, but it can be improved with additional information that make it more clear to users, and become a good answer to the original question. \$\endgroup\$ – liggiorgio Jun 2 '18 at 15:12
  • 1
    \$\begingroup\$ Also: it looks like you've created at least three separate accounts under the same name. Do you need any help navigating the profile creation, to ensure you get credit for all your contributions on one account? \$\endgroup\$ – DMGregory Jun 2 '18 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.