- Home /
Help make this timer work?
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.
Is it possible that your On$$anonymous$$ouseDown() function is sending you to the "Lose" scene? If that happens to be called during the first few frames, what would happen?
It looks to me like if On$$anonymous$$ouseDown() is called while Commands.Command is anything other than 1, you'll instantly load the "Lose" scene.
I switched the onmouse down and the void update round, no effect
You should take the time to format your code (and develop a consistent style). Not just for the our sake, but for your own. Tidy code is much easier to read and debug.
Answer by raimon.massanet · May 14, 2013 at 12:35 PM
I am not sure of what you are trying to do, your code is a bit messy, BUT:
You seem to be assuming that Command is an integer, when it is actually a random float, so it will hardly ever have the exact values of 1, 2, 3 or 4.
For that reason, whenever you click on a Mouth object, it will load the Lose scene, because Command will hardly ever be 1.
You should round the result of Random.Range if you want it to be an integer:
Mathf.Round(Random.Range(0,4));
PS: By the way, I think you have a curly bracket mess in the Update method of the Mouth class.
Answer by aldonaletto · May 14, 2013 at 12:33 PM
Apparently, your timeout variable is set to low in the Mouth script, just 1.65 seconds. If Commands.Command is 1, the Mouth script loads the Lose level directly after just 1.65 seconds!
The plan with the game is that the commands will change/appear at different times. As the game progresses the times will get shorter.
I want to be able to say the game loads the lose scene if the player doesn't touch that command before the next one appears.
Agreed it does need some cleaning :(