Hey guys,
Trying to get a timer working on my game.
The point of the timer is, that when it runs out the player will lose.
Here is the script where I am trying to initiate the timer
using UnityEngine;
using System.Collections;
public class Mouth : MonoBehaviour {
GameObject other;
public AudioClip MouthReaction;
float timeout = 1.65f;
private float timer = 0.0f;
void Start (){
other = GameObject.FindGameObjectWithTag("Commands");
}
void OnMouseDown (){
if(Commands.Command == 1)
{
other.GetComponent<Commands>().Respawn();
audio.PlayOneShot(MouthReaction);
Score.GameScore += 50;
}
else
Application.LoadLevel ("Lose");
print("You Lose");
}
void Update()
{ timer += Time.deltaTime;
{ if (Commands.Command == 1)
if (timer > timeout)
Application.LoadLevel ("Lose");
}
}
}
And here is the other script it is calling to
using UnityEngine;
using System.Collections;
public class Commands : MonoBehaviour {
public float Speed = 0.5f;
public static int Command = 0;
public static int LastCommand = 0;
public float TimeLeft = 0;
public Texture2D mouth;
public Texture2D eye;
public Texture2D nose;
public Texture2D ear;
private Texture2D myimage;
public AudioClip MouthCommand;
public AudioClip EyeCommand;
public AudioClip EarCommand;
public AudioClip NoseCommand;
void OnGUI (){
GUI.DrawTexture(new Rect(Screen.width / 2, Screen.height -120, 200,100), myimage);
}
void Start (){
Command = Random.Range(0,5);
}
void Update (){
TimeLeft += Time.deltaTime;
if(TimeLeft >2)
{
Respawn();
}
transform.Translate(Vector3.up * Time.deltaTime * Speed); // Translate it up
if(transform.position.y >= 1.65f) // Respawn it
{
Respawn();
}
if(LastCommand != Command)
{
LastCommand = Command;
switch(Command)
{
case 1:
myimage = mouth;
audio.PlayOneShot(MouthCommand);
break;
case 2:
myimage = eye;
audio.PlayOneShot(EyeCommand);
break;
case 3:
myimage = nose;
audio.PlayOneShot(NoseCommand);
break;
case 4:
myimage = ear;
audio.PlayOneShot(EarCommand);
break;
}
}
}
public void Respawn (){
TimeLeft = 0;
transform.position = new Vector3(Random.Range(0.2f,0.9f), 0.4f, transform.position.z);
Command = Random.Range(1,5);
}
}
The problem with this scripting is that it straight away goes to the “Lose” scene. I want the timer to be the same amount of time before the next command would spawn.