Audio not working on first run of game, but after second run it does
Hi,
In my game I have a television. I have a box collider on it with a trigger. The television screen has a black texture. When I enter the trigger I can turn on the tv and the black texture is replaced by a movie. Everything is working fine. Only the audio is not playing. But is I stop Unity player and run the game again, the audio is working. I didn't try to compile the game to see if the problem is also there. I only tested it inside Unity itself. Every time I restart Unity, on the first run of the game, I have this problem. But every run after the first one, it's working fine. I'm a little afraid that when I compile the game I have this problem. But unlike Unity, when you start the game, you can't use the "play" "pauze" buttons to run the game a second time. If the game is started, it is always a first run.
This is the script I'm using:
using UnityEngine;
using System.Collections;
public class tvdoenspelen : MonoBehaviour
{
public Material[] myMaterials = new Material[5];
int maxMaterials;
int arrayPos = 0;
bool drawGUI = false;
bool tvspeelt = false;
AudioSource audio;
void Start ()
{
maxMaterials = myMaterials.Length-1;
audio = GetComponent<AudioSource> ();
}
void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "Player")
{
drawGUI = true;
}
}
void OnTriggerExit(Collider col)
{
drawGUI = false;
}
void OnGUI ()
{
if (drawGUI == true && tvspeelt == false)
{
GUI.Box (new Rect (Screen.width * 0.5f, Screen.height * 0.5f, 200, 22), "Drop op E om TV te kijken");
}
if (drawGUI == true && tvspeelt == true)
{
GUI.Box (new Rect (Screen.width * 0.5f, Screen.height * 0.5f, 200, 22), "Drop op E om TV af te zetten");
}
}
void Update ()
{
if (drawGUI == true && Input.GetKeyDown(KeyCode.E))
{
GetComponent<Renderer>().material = myMaterials[arrayPos];
if(arrayPos == maxMaterials)
{
arrayPos = 0;
tvspeelt = false;
audio.mute = true;
}
else
{
arrayPos++;
tvspeelt = true;
Renderer r = GetComponent<Renderer> ();
MovieTexture movie = (MovieTexture)r.material.mainTexture;
movie.Play ();
audio.mute = false;
}
}
}
}
And this is my inspector of my TV screen: