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 Chris 4 · Apr 29, 2011 at 08:58 PM · collisiontimerresetbce0019bce0005

Add seconds to timer countdown.

Okay so I have a pretty basic countdown timer here that counts down x # of seconds. It works to countdown:). The problem is I want to add x amount of seconds to the time when you hit a gameobject tagged timed. The problem is I get errors so something is not right. (I am still new to scripting)

private var startTime; private var restSeconds : int; private var roundedRestSeconds : int; private var displaySeconds : int; private var timed = false; private var displayMinutes : int; var countDownSeconds : int; function Awake() { startTime = Time.time; } function OnGUI () { // I need to make sure that my time is based on when this script was first called //instead of when my game started var guiTime = Time.time - startTime; restSeconds = countDownSeconds - (guiTime); //display messages or whatever here -->do stuff based on your timer if (restSeconds == 60) { print ("One Minute Left"); } if (restSeconds == 0) { Application.LoadLevel(loadedLevel); //I may add more later } //display the timer roundedRestSeconds = Mathf.CeilToInt(restSeconds); displaySeconds = roundedRestSeconds % 60; displayMinutes = roundedRestSeconds / 60; text = String.Format ("{0:00}:{1:00}", displayMinutes, displaySeconds); GUI.Label (Rect (400, 25, 100, 30), text); }

function OnControllerColliderHit(Hit : ControllerColliderHit) { if(Hit.gameObject.tag == "fallout2") { timed = true; } }

function LateUpdate() { if(timed) { restSeconds == 60; timed = false; Destroy(OnControllerColliderHit.gameObject); } }

That is my code. As seen above I also have a problem with resetting the level. It does not want to load. Man I feel stupid when it comes to scripting. Anyway thanks for your time. :)

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 Joshua · Apr 29, 2011 at 09:10 PM 0
Share

I don't see an obvious error, but I'd replace display$$anonymous$$inutes = roundedRestSeconds / 60; with display$$anonymous$$inutes = (roundedRestSeconds-displaySeconds) / 60; for more exact results.

avatar image xCRKx TyPHooN · Apr 29, 2011 at 09:27 PM 0
Share

Can you post the errors you received?

avatar image Chris 4 · Apr 30, 2011 at 03:40 AM 0
Share

23,23 $$anonymous$$ identifier: 'loadedlevel' 46,33 'gameObject' is not a member of 'function(UnityEngine.ControllerColliderHit): void'.

3 Replies

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

Answer by Uzquiano · Apr 29, 2011 at 09:37 PM

Hi,

When you reset you should change this:

restSeconds = 60;

And about the LoadLevel error, you should define loadedLevel as :

var loadedLevel : int; 
loadedLevel = 1;//for example 1

Take a look to this. If you want to load the scenes as strings, a string is always between "Helo", for example:

var loadedLevel : String; 
loadedLevel = "NameoftheLevel";

You should change here, not inside the if() ;)

function LateUpdate()
{
    if(timed)
    {
    restSeconds = 60;
    timed = false;
    Destroy(OnControllerColliderHit.gameObject); 
    }
}  
Comment
Add comment · Show 3 · 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 Chris 4 · Apr 30, 2011 at 03:41 AM 0
Share

That removed one of the errors :)

avatar image Chris 4 · May 01, 2011 at 07:41 PM 0
Share

I now have 7 errors 1. Assets/New Folder 2/timer.js(37,10): BCE0044: expecting (, found 'OnControllerColliderHit'. 2. Assets/New Folder 2/timer.js(37,38): BCE0044: expecting ), found ':'. 3. Assets/New Folder 2/timer.js(37,39): UCE0001: ';' expected. Insert a semicolon at the end. 4. Assets/New Folder 2/timer.js(37,61): BCE0043: Unexpected token: ). 5. Assets/New Folder 2/timer.js(39,1): BCE0043: Unexpected token: if. 6. Assets/New Folder 2/timer.js(39,37): UCE0001: ';' expected. Insert a semicolon at the end. 7. Assets/New Folder 2/timer.js(41,7): BCE0044: expecting :, found '='.

avatar image Chris 4 · May 02, 2011 at 07:22 PM 0
Share

I chose to remove the Destroy the gameObject command and it removed the error. The code you did made the load level work. :) The problem is when I collide into the gameobject it does nothing. I have box colliders on both game object and I even tried as the gameObject box collider as ''is triggered'' but that does not seem to help. Any ideas?

avatar image
1

Answer by sneftel · Apr 29, 2011 at 09:42 PM

That is some really complicated code for what you're trying to do. :-) Anyhoo, the basic problem here is that even though restSeconds appears to be a class variable, you overwrite its value in OnGUI, so it doesn't actually lead to any extra information being kept around.

Try this: remove startTime entirely, and make restSeconds a float. In Awake() (or, depending on what you want, in Reset()), set restSeconds to countDownSeconds. In OnGUI(), remove the guiTime variable and don't change restSeconds. (Also, for the one minute warning, use RoundedRestSeconds.) In Update() or LateUpdate(), decrement restSeconds by the value of Time.deltaTime. To increase the amount of time left, just add to restSeconds.

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 Chris 4 · May 02, 2011 at 10:45 PM

I figured it out! However I could not have done it without the help of Uzquiano so I am going to put him as the good answer. I changed the code so that when you hit the collider of the object tagged timed it resets the timer to 40 seconds (The starting time) Thank you all!:)

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

No one has followed this question yet.

Related Questions

How To Reset Timer on a Collision (C#) 0 Answers

Timer reset after elapsed time. 1 Answer

Resetting Combos 1 Answer

The timer starts after a collision with an object 1 Answer

Why is this Reset code not working? 3 Answers


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