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 laradov · Apr 11, 2015 at 03:45 PM · functioncallonce

Efficient way for calling a function once

Hi, I know there are some ways for calling a function once. The most common one:

 bool functionCalled = false;
 
     void Update () {
 
         if (!functionCalled && AfterSomeTime) {
             doSomething ();
             functionCalled = true;
         }
     }

However this is not efficient since the if statement checks during the whole game. After the function is called, you just waste processing power with the always-false if statement. It may seem insignificant but it may accumulate if you use that a lot. Is there an efficient way for calling a function once?

Comment
Add comment · Show 2
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 fafase · Apr 11, 2015 at 03:51 PM 0
Share

Based on how AtferSomeTime is set you can pass a delegate or simply call your method once it is done.

One slight improvement already would be to place the AfterSomeTime first since it is the one most likely to be false at first. But all that is micro optimization. A bool check will not change much of your app, even if you have hundreds of them.

But, for your question, you should let us know how AfterSomeTime is set and from there, it is easier to say.

avatar image laradov · Apr 11, 2015 at 03:59 PM 0
Share

Let's say

   if (!functionCalled && Time.realtimeSinceStartup>10)

placing it first will be worse in this case since it will be true forever after function call.

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by fafase · Apr 11, 2015 at 04:13 PM

Use Invoke with a timer so that Unity takes care of the optimization:

 void Start(){
    Invoke("doSomething ", 10f);
 }

This way not only you don't have to care about the check but you don't need the boolean anymore.

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
1

Answer by KalleH · Apr 11, 2015 at 03:55 PM

If statemenets don't really take any time at all so you shouldn't be worried of them even if you had plenty of them. The biggest problem is probably just that the code itself might look a bit messy but it shouldn't have any major impact on the performance. The way you used is probably the best one for this (please someone correct me if there is another way but I'm 99% sure that this is the way to go). Also you could use Start and Awake functions if the code has to be executed in the beginning of the game.

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 dynamoo21 · Feb 04, 2017 at 07:13 AM

It all depends on your requirement, Updates are best suited for taking constant input for constant output. Another alternative is to use coroutines for example.

 bool isActionPerformed;
 
     IEnumerator CoroutineName(){
 
         while(!isActionPerformed){
             yield return new WaitForEndOfFrame ();
             //Logic here
 
             //toggle the boolean whenever you acheive your objective
             isActionPerformed = true;
         }
     }

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

20 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

Related Questions

Calling A Function Once 0 Answers

Calling a function only once in Update 3 Answers

Events called every Nth day 1 Answer

Call function after script compilation? 2 Answers

How to call a function from a script component on another object 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