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