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 Florensie · Nov 28, 2014 at 04:56 PM · timer-scriptreturn value

How do I return info to the script I called a function from?

So I made this handy little timer script which will be called from another script:

 using UnityEngine;
 using System.Collections;
 
 public class Timer : MonoBehaviour {
 
     private float StopTime = Time.time;
     private bool Running = false;
 
     public void test(float SecondsToWait){
         Running = true;
         StopTime = Time.time + SecondsToWait;
 
         while(Running == true){
             if(Time.time == StopTime){
                 Running = false;
             }
         }
     }
 }

But now I want it to return it has finished timing. Can anyone tell me how?

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 koray1396 · Nov 28, 2014 at 05:23 PM 0
Share

In addition to Owen, you should really work on basics of program$$anonymous$$g.

Stop time will always be Time.time + SecondsToWait. How can it be equal to Time.time?

So you would like to do this;

 IEnumerator test(float SecondsToWait){
     Running = true;
     float counter = 0f;
     while(Running){
         counter += Time.deltaTime;
         if(counter >= SecondsToWait){
             Running = false;
             yield break;
         }
         yield return null;
     }
 }

avatar image Owen-Reynolds · Nov 28, 2014 at 05:37 PM 0
Share

koray: Using Time.time like the OP did, does work, since Unity increases it for you. The idea is, you want to for wait 3 seconds and Time.time is now 105. So you record 108 as the stopTime. Then the loop simply waits for Time.time to reach 108. It saves having to make the extra counter, and adding to it.

But the rest looks fine, to me.

But, again, WaitForSeconds is already written and does the same thing.

avatar image Florensie · Nov 28, 2014 at 05:46 PM 0
Share

Thanks everybody It worked. I used the waitforseconds thing.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Owen-Reynolds · Nov 28, 2014 at 05:09 PM

?? That loop will just freeze your program. It needs yield return null; and to be in a coroutine. Beyond that, the == will always be false (time will jump from just under, to just over. Use <=.) Why are stopTime and running globals? What if two people want to time something?

And why not just use the built-in WaitForSeconds? Or, in many cases, just the built-in Invoke.

I'd restart with what the end result is, and look from there.

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 Mapleman · Dec 08, 2014 at 06:40 PM

I had quite the same problem. I ended up writing a bit more generic solution. So what I did, I created a persistent gameobject to handle 'wait & execute something'- kind of pattern. Here how it goes...

 using UnityEngine;
 using System.Collections;
 
 public delegate void TimerCommand();
 
 public class MyTimer : MonoBehaviour {
 
     private static TimerCommand _timerCallBack;
     private static float _targetTime;
     private static bool _isRunning = false;
 
     void Awake()
     {
         DontDestroyOnLoad(this);
     }
 
     public static void StartMyTimer(float time,TimerCommand callBack)
     {
         //Ensure that last StartMyTimer call has finished
         if(_isRunning)
             return;
 
         _timerCallBack = callBack;
         _targetTime = Time.realtimeSinceStartup+time;
         _isRunning = true;
     }
 
     // Update is called once per frame
     void Update () 
     {
         if(_isRunning && Time.realtimeSinceStartup > _targetTime)
         {
             _isRunning = false;
             _timerCallBack();
         }
     }
 }

So create an empty gameobject and attach the script to it. If you wish, create a prefab.

My need in this case was to tune down the volume of my background music while another short music clip was played. So here's an example how to use this. What it does, it waits for 2 seconds and then resets the background music volume to 1.0f.

         //Note the lambda expression syntax. 
         //Of course any method matching the delegate declared in the MyTimer will do
         MyTimer.StartMyTimer(2.0f, 
                              //the callback which is called when timer has finished
                              () => { audio.volume = 1.0f;});


Obviously the mytimer script can be tweaked to take multiple timed events, set up periodic 'events' etc. But for now, this was sufficient for my needs. Hope this helps.

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

29 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I make my timer continue to count? (C# Script) 2 Answers

Timer Start on Gui from function 0 Answers

How to change leaf textures over time (For changing seasons) 1 Answer

Return value from coroutine 2 Answers

Variable problem in Javascript. 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