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
0
Question by robounited · Dec 18, 2012 at 05:05 PM · triggertimerloadlevel

Why does my game go straight to game over when game starts?

I have a trigger that starts a timer and i want it to load my game over screen when it gets to 0 but before i can play the game to activate the trigger it goes to the game over screen. this is my code:

 var AlarmSetOff = false;
 var Alarm : AudioClip;
 var endTime : float;
 var textMesh : GUIText;
 
 function OnTriggerEnter (other : Collider) {
     AlarmSetOff = true;
     endTime = Time.time + 60;
     textMesh = GameObject.Find ("Timer").GetComponent(GUIText);
     textMesh.text = "60";
 }
 
 function OnGUI () {
 if (AlarmSetOff){
     audio.PlayOneShot(Alarm);
     }
 }
 
 function Update()
 {
     var timeLeft : int = endTime - Time.time;
     if (timeLeft < 0){ timeLeft = 0;
     textMesh.text = timeLeft.ToString();
     OpenLevel("GameOverScreen");}
 }
 
 function OpenLevel(level : String){
     Application.LoadLevel(level);
 }
 
 @script RequireComponent(AudioSource)

Why does this happen, any ideas ??

Comment
Add comment · Show 1
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 Loius · Dec 18, 2012 at 05:27 PM 0
Share

You'll need to re-paste that code and use the code button (101-010 square) to format it so it's readable.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Dec 18, 2012 at 06:52 PM

The answer is quite simple. You have a timeout variable which get set to the current time + 69 when you enter the trigger. This will effectively "wait" 60 sec. from the point where you enter the trigger, before the gameover screen is displayed.

However you forgot something important. What's the initial value of you timeout? Right, it's 0. That means when you start your game, endtime - Time.time will be immediately smaller than 0.

There are two solutions for this:

  • initialize the endTime variable with a really big number (you could also use "float.PositiveInfinity")

  • Use an additional boolean flag which indicates that you have already entered the trigger.

The second version is the most robust one. Just imagine what happens in the first case when you reenter the trigger several times. The timeout would restart. Using a flag you can pervent this. You actually already have a boolean (AlarmSetOff) which you can use:

 var AlarmSetOff = false;
 var Alarm : AudioClip;
 var endTime : float;
 var textMesh : GUIText;
 
 function OnTriggerEnter (other : Collider)
 {
     if (!AlarmSetOff)   // Not triggered yet? --> trigger it
     {
         AlarmSetOff = true;
         endTime = Time.time + 60;
         textMesh = GameObject.Find ("Timer").GetComponent(GUIText);
         textMesh.text = "60";
     }
 }
 
 function Update()
 {
     if (AlarmSetOff)
     {
         audio.PlayOneShot(Alarm);
         var timeLeft : int = endTime - Time.time;
         textMesh.text = timeLeft.ToString("00");
         if (timeLeft < 0)
         {
             timeLeft = 0;
             AlarmSetOff = false; // reset the condition
             OpenLevel("GameOverScreen");
         }
     }
 }
 
 function OpenLevel(level : String)
 {
     Application.LoadLevel(level);
 }
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 Loius · Dec 18, 2012 at 05:28 PM

It looks like you have no brackets on your if. If only applies to the very next command (essentially, using curly brackets makes a set of commands into one 'block'. there's more exact words for it but that's the gist). In Python and related languages, tabbing things over counts as blocks, but in C and Unityscript, whitespace is ignored.

You want:

 if ( condition ) {
   statement1;
   OpenLevel(blah);
 }
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

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

11 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

Related Questions

Making a loadlevel script that saves a lot of space 1 Answer

Destroy trigger after a certain time 2 Answers

End Game when hit's water 2 Answers

How to load a level after 30 seconds? 3 Answers

How to stop a Countdown Timer? 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