(SOLVED) Audio play problem?

I have two audio sources in my main camera. First one is plays at wakeup and that works fine. Second is in a (empty) child object of the main camera. It is called in my Player script (C#) and it works in void Start(), but not in my if -statement in Update(). I can’t figure out why. Help?

Line 33-35 is commented out in the script, but it tells you what it does. I made an separate function (void PlayVictory()) for it and it’s down the script. It works on Start(), but not where I want it to work (line 86)

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PlayerController : MonoBehaviour {

    public float speed;
    public float jumpForce = 700f;
    public GUIText ScoreText;
    public int count;
    public List<GameObject> BombArray;
    public GUIText highscore;
    private Vector2 movement;
    private Animator myAnimation;
    public static bool IsBurning;
    public AudioSource myAudio;
    public GUIText wintext;
    AudioSource audioSource;
    public AudioClip clickClip;



    void Awake() {
        count = (PlayerPrefs.GetInt("Player Score"));
    }


    // Use this for initialization
    void Start () {
        myAnimation = GetComponent<Animator> ();
        IsBurning = false;
        wintext.enabled = false;
        //audioSource = Camera.main.transform.Find("VictorySong").GetComponent<AudioSource>();
        //audioSource.clip = clickClip;
        //audioSource.Play ();
        PlayVictory ();

    }
  
    // Update is called once per frame
    void Update () {
        float inputX = Input.GetAxis("Horizontal");
        movement = new Vector2(speed * inputX,0);

        //Rajoitetaan pelaajan liike kenttään
        if (transform.position.x <= -6f) {
            transform.position = new Vector2(-6f, transform.position.y);
        } else if (transform.position.x >= 2.8f) {
            transform.position = new Vector2(2.8f, transform.position.y);
        }
        if (transform.position.y <= -4.2f) {
            transform.position = new Vector2(transform.position.x, -4.2f);
        } else if (transform.position.y >= 4.2f) {
            transform.position = new Vector2(transform.position.x, 4.2f);
        }

        //Laitetaan pelaaja liikkeelle
        if (Input.GetKey ("left")) {
                        myAnimation.SetBool ("jack_left", true);
                } else {
                        myAnimation.SetBool ("jack_left", false);
                }

        if (Input.GetKey ("right")) {
                        myAnimation.SetBool ("jack_right", true);
                } else {
                        myAnimation.SetBool ("jack_right", false);
                }

        if (Input.GetKey ("space")) {
                        myAnimation.SetBool ("jack_up", true);
                        rigidbody2D.gravityScale = 0;
                        rigidbody2D.AddForce (new Vector2 (0, jumpForce));
                } else {
                        myAnimation.SetBool ("jack_up", false);
                        rigidbody2D.gravityScale = 40;
                }

        if (Input.GetKey("escape"))
            Application.Quit();

        if (BombArray.Count == 0) {
            wintext.enabled = true;
            PlayerPrefs.SetInt("Player Score", count);
            myAudio.Stop();
            //PlayVictory();
            Invoke("LoadLevel",9);
        }

    }

    void FixedUpdate () {
        //pelaaja liikkeelle
        rigidbody2D.velocity = movement;
        //tulosta tauluun
        ScoreText.text = count.ToString("000000");  
    }

    void OnCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.tag == "Bombs") {

            if (IsBurning == false) {
                for (int i = 0; i < BombArray.Count; i++) {
                    if (col.gameObject == BombArray[i]) {
                        BombArray[i+1].GetComponent<BombBurn>().enabled = true;
                        BombArray.RemoveAt(i);
                        IsBurning = true;
                    }
                }
            }

            else if (IsBurning == true) {
                for (int i = 0; i < BombArray.Count; i++) {
                    if (col.gameObject == BombArray[i]) {
                        BombArray.RemoveAt(i);
                        IsBurning = false;
                    }
                }
            }

            if (IsBurning == false) {
                for (int i = 0; i < BombArray.Count; i++) {
                    if (col.gameObject != BombArray[i]) {
                        BombArray[0].GetComponent<BombBurn>().enabled = true;
                        IsBurning = true;
                    }
                }
            }

            Destroy (col.gameObject);
            count += 200;
        }
              
    }

    void OnLevelWasLoaded(int level) {
        if (level == 1) {
            count = 0;
            }
    }

    void LoadLevel() {
        int i = Application.loadedLevel;
        Application.LoadLevel(i + 1);
        }

    void PlayVictory() {
        audioSource = Camera.main.transform.Find("VictorySong").GetComponent<AudioSource>();
        audioSource.clip = clickClip;
        audioSource.Play();
    }
}

You should not store audio clips like this. The reason why, is you won’t have any control of audio clips, and if multiple objects require sound are you really going to have it search for the clip? This will have a performance impact on your game. I suggest you make an audio manager, to manage your game audio for best performance.

I have been coding / using Unity for about 2 weeks and I have started to learn coding about 4 months ago… Give me a break… sir :slight_smile:

Ah I see, no worries

Here try this custom package. I made it myself, there was an other fellow here that needed help so I made this. If you have any questions on how to use it just PM me.

I’ve got it! Now it’s working. Didn’t use Audio Manager for now. Just an easy little fix for my needs for now. Here’s my “final” code. Project is still under developement, but audio issue solved for now. (Lines 33-35 and 85-88)

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PlayerController : MonoBehaviour {

    public float speed;
    public float jumpForce = 700f;
    public GUIText ScoreText;
    public int count;
    public List<GameObject> BombArray;
    public GUIText highscore;
    private Vector2 movement;
    private Animator myAnimation;
    public static bool IsBurning;
    public AudioSource myAudio;
    public GUIText wintext;
    AudioSource audioSource;
    public AudioClip clickClip;
    private bool soundplayed;


    void Awake() {
        count = (PlayerPrefs.GetInt("Player Score"));
    }


    // Use this for initialization
    void Start () {
        myAnimation = GetComponent<Animator> ();
        IsBurning = false;
        wintext.enabled = false;
        audioSource = Camera.main.transform.Find("VictorySong").GetComponent<AudioSource>();
        audioSource.clip = clickClip;
        soundplayed = false;

    }
  
    // Update is called once per frame
    void Update () {
        float inputX = Input.GetAxis("Horizontal");
        movement = new Vector2(speed * inputX,0);

        //Rajoitetaan pelaajan liike kenttään
        if (transform.position.x <= -6f) {
            transform.position = new Vector2(-6f, transform.position.y);
        } else if (transform.position.x >= 2.8f) {
            transform.position = new Vector2(2.8f, transform.position.y);
        }
        if (transform.position.y <= -4.2f) {
            transform.position = new Vector2(transform.position.x, -4.2f);
        } else if (transform.position.y >= 4.2f) {
            transform.position = new Vector2(transform.position.x, 4.2f);
        }

        //Laitetaan pelaaja liikkeelle
        if (Input.GetKey ("left")) {
                        myAnimation.SetBool ("jack_left", true);
                } else {
                        myAnimation.SetBool ("jack_left", false);
                }

        if (Input.GetKey ("right")) {
                        myAnimation.SetBool ("jack_right", true);
                } else {
                        myAnimation.SetBool ("jack_right", false);
                }

        if (Input.GetKey ("space")) {
                        myAnimation.SetBool ("jack_up", true);
                        rigidbody2D.gravityScale = 0;
                        rigidbody2D.AddForce (new Vector2 (0, jumpForce));
                } else {
                        myAnimation.SetBool ("jack_up", false);
                        rigidbody2D.gravityScale = 40;
                }

        if (Input.GetKey("escape"))
            Application.Quit();

        if (BombArray.Count == 0) {
            wintext.enabled = true;
            PlayerPrefs.SetInt("Player Score", count);
            myAudio.Stop();
            if (!soundplayed) {
                audioSource.Play();
                soundplayed = true;
            }
            Invoke("LoadLevel",9);
        }

    }

    void FixedUpdate () {
        //pelaaja liikkeelle
        rigidbody2D.velocity = movement;
        //tulosta tauluun
        ScoreText.text = count.ToString("000000");  
    }

    void OnCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.tag == "Bombs") {

            if (IsBurning == false) {
                for (int i = 0; i < BombArray.Count; i++) {
                    if (col.gameObject == BombArray[i]) {
                        BombArray[i+1].GetComponent<BombBurn>().enabled = true;
                        BombArray.RemoveAt(i);
                        IsBurning = true;
                    }
                }
            }

            else if (IsBurning == true) {
                for (int i = 0; i < BombArray.Count; i++) {
                    if (col.gameObject == BombArray[i]) {
                        BombArray.RemoveAt(i);
                        IsBurning = false;
                    }
                }
            }

            if (IsBurning == false) {
                for (int i = 0; i < BombArray.Count; i++) {
                    if (col.gameObject != BombArray[i]) {
                        BombArray[0].GetComponent<BombBurn>().enabled = true;
                        IsBurning = true;
                    }
                }
            }

            Destroy (col.gameObject);
            count += 200;
        }
              
    }

    void OnLevelWasLoaded(int level) {
        if (level == 1) {
            count = 0;
            }
    }

    void LoadLevel() {
        int i = Application.loadedLevel;
        Application.LoadLevel(i + 1);
        }

}