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 Rydrako · Apr 22, 2013 at 11:32 PM · c#ifdeltatimetime.time

Small Timer in C#?

Because I don't do c# much but anyways I'm trying to do this thing where it will destroy after 2 seconds when it adds time. using UnityEngine; using System.Collections;

public class BossAnimation : MonoBehaviour {

 // Use this for initialization
 public bool animate = false;
 public GameObject boss;
 public Camera cam;
 public GameObject bubbles;
 public double NumberOfSecondsToWait;
 private bool IsTiming = false;
 private double timer;
 
 void Update () 
 {
         if(animate)
         {
                 iTween.MoveTo(gameObject, iTween.Hash("path", iTweenPath.GetPath("Boss Path"), "orienttopath", true, "time", 120));
                 animate = false;
         }
     
         if(transform.position.z < -40)
         {
                 boss.transform.position = new Vector3(-95,-18,-40);
                 Destroy(bubbles.gameObject);
                 cam.camera.enabled = true;
                 beginTimer ();
         }
     
         if(IsTiming)
         {
                 timer += Time.deltaTime;
         }
     
         if (timer > NumberOfSecondsToWait) //This wont call...
         {
                 cam.camera.enabled = false;
                 Destroy (gameObject);
                 print ("Time has passed!");
         }
 }
 
 void beginTimer()
 {
     timer = 0;
     IsTiming = true;
 }

}

It all works until I check for the time. I'm sure the answer is pretty simple but I'm not good with c# logic but I have to make this work in c# so my iTween path works. Help would be great!

Comment
Add comment
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

2 Replies

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

Answer by sparkzbarca · Apr 22, 2013 at 11:43 PM

time.time is the amount of time in seconds since the game started. Nothing could ever be less than that or at least not

time.time + 2.0 could ever be.

If you want to make a simple timer you need to use time.deltatime

deltatime is the time in seconds between frames (a very small number)

so

 void beginTimer()
 {
 timer = 0;
 IsTiming = true;
 }
 
 void update(){
 if(isTiming)
 {
 //+= is the same thing as adding to the current variable
 //timer = timer + time.delatime is the same thing as time +=... its just faster to use +=
 
 timer += time.deltatime;
 
 }
 
 if (timer > NumberOfSecondsToWait)
 {
 //do something, like destroy;
 }
 
 }
 
 void EndTimer(){
 IsTiming = false;
 }

thats just for general knowledge

you can already destroy soemthing in a few seconds time

Destroy(gameobject, time in seconds to delay destruction)

so

Destroy(gameobject,5f)

would destroy the game object in 5 seconds.

Still useful to know how to make a timer though.

mark as answered please

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 sajithsathes · Apr 18, 2020 at 06:07 AM 0
Share

i just wanted to make sure, does while loop help in such cases and by help i mean is it more efficient or something like that?

 while (isTi$$anonymous$$g) {
     timer += Time.deltaTime;
     if (timer >= 2.0f) {
         isTi$$anonymous$$g = false;
         //do whatever thats needed here
         break;
     }
 }

wud this piece of code make sense?

avatar image
0

Answer by codecranker · Apr 23, 2013 at 04:31 AM

I see that your class extends from MonoBehavior, in that case you can also use Invoke() and InvokeRepeating(). You can always check their documentation on unity script reference.

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

14 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

If statement not working correctly. 3 Answers

if function broken? 3 Answers

if a string has certain letter(s) or number(s) 2 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