Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by 4shm3x · May 13, 2013 at 11:19 PM · timer

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rutter · May 13, 2013 at 11:30 PM 0
Share

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.

avatar image 4shm3x · May 14, 2013 at 06:47 AM 0
Share

I switched the onmouse down and the void update round, no effect

avatar image bdjnk · May 14, 2013 at 06:51 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image 4shm3x · May 14, 2013 at 02:54 PM 0
Share

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 :(

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

I made a better shader how do i fix[add _Shadow Strength]help???>Sorry that im asking for to much 1 Answer

Unity3D Timer. 1 Answer

How do i add and check a timer 1 Answer

Creating a timer for combat (C#) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges