Hello Unity Answers,
I’m having a problem with the sound when the question is displayed on screen.
When I collide into the first box the question appears on screen and the countdown sound begins to play and if i press a button it will stop. Which is great as this works how I want.
However If I approach the second question box, the countdown sound doesn’t play upon collision but will only play when I select an answer? I have exactly the same code for Question 1, in the Question 2 class so not sure what is wrong.
All advice & suggestions welcome.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Trigger : MonoBehaviour
{
public GameObject guiObject;
private Question1 guiScript;
public AudioSource cubeAudio;
public AudioSource countdownAudio;
void Awake ()
{
guiScript = guiObject.GetComponent<Question1> ();
guiScript.enabled = false; //Turn off the script, so the OnGUI function doesn't draw the question window
}
void OnTriggerEnter (Collider other)
{
if (cubeAudio != null) cubeAudio.Play();
if (countdownAudio != null) countdownAudio.Play();
guiScript.enabled = true;
Destroy (this.gameObject);
}
}
Question 1 code
using System;
using System.Collections.Generic;
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Question1 : MonoBehaviour {
private Rect windowRect = new Rect (500, 100, 400, 200); //Window size
public bool question1;
private int count;
public Text countText;
public AudioSource countdownAudio;
private bool showTimer = true;
private float Timer = 10f;
void start()
{
count = 0;
SetCountText ();
}
void FixedUpdate(){
if (showTimer == true) {
Timer -= Time.deltaTime;
}
if (Timer <= 0f) {
showTimer = false;
Timer = 10f;
Destroy (this.gameObject);
Application.LoadLevel (Application.loadedLevel);
}
}
void OnGUI(){
{
windowRect = GUI.Window (0, windowRect, WindowFunction, "Ebola Quiz Island"); //window on screen
}
}
void WindowFunction (int windowID)
{
GUI.Label (new Rect (30, 25, 200, 50), " What year did Ebola begin?"); // Question
if (GUI.Button (new Rect (20, 100, 100, 30), "1976")) // Correct Answer
{
AudioSource source = countdownAudio.GetComponent<AudioSource>();
source.mute = !source.mute;
Destroy (this.gameObject);
count = count += 1;
SetCountText ();
}
if (GUI.Button (new Rect (280, 100, 100, 30), "1986")) //Wrong answer
{
AudioSource source = countdownAudio.GetComponent<AudioSource>();
source.mute = !source.mute;
Destroy (this.gameObject);
//Application.LoadLevel(Application.loadedLevel);
}
if (GUI.Button (new Rect (20, 150, 100, 30), "1996")) // wrong answer
{
AudioSource source = countdownAudio.GetComponent<AudioSource>();
source.mute = !source.mute;
Destroy (this.gameObject);
//Application.LoadLevel(Application.loadedLevel);
}
if (GUI.Button (new Rect (280, 150, 100, 30), "1966")) // wrong answer
{
AudioSource source = countdownAudio.GetComponent<AudioSource>();
source.mute = !source.mute;
Destroy (this.gameObject);
//Application.LoadLevel(Application.loadedLevel);
}
if (showTimer == true)
{
GUI.Label (new Rect (300, 25, 200, 50), "Timer: " + Timer.ToString ("F0"));
}
}
void SetCountText()
{
ValuesHolder.answersCount++;
countText.text = "Score: " + ValuesHolder.answersCount.ToString();
}
}