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 OctoSloths · Mar 11, 2015 at 10:44 PM · c#functionboolcallvoid

Check if a function is no longer being called?

So I have a function being called and I was wondering if there is a way to check if it's no longer being called so I can set a bool to false? Here is a part of my script:

 void Update (){
         startPos = new Vector3(transform.position.x, startHeight, transform.position.z);
         if(goUp){
             transform.position = Vector3.MoveTowards (transform.position, (startPos + new Vector3(0, 5, 0)), Time.deltaTime*speed);
         }
         else
             transform.position = Vector3.MoveTowards (transform.position, startPos, Time.deltaTime*speed);
     }
     public void OnPressUp () {
         //goUp=!goUp;
         goUp = true;
     }

I want to check if OnPressUp is no longer being called so I can set the bool goUp to false.

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

3 Replies

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

Answer by Baste · Mar 11, 2015 at 10:50 PM

A function that's not async or a coroutine will always be finished unless the program flow is actually in the function. When you do something like this:

 int x = 5;
 DoAThing();
 int y = 10;

Everything in DoAThing will be executed before the int y = 10 line is reached. So it doesn't make any sense to check if your "OnPressUp" function isn't being called any more, as it's not being called anywhere outside itself.

Comment
Add comment · Show 2 · 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 OctoSloths · Mar 11, 2015 at 10:54 PM 0
Share

What would you suggest ins$$anonymous$$d?

avatar image Owen-Reynolds · Mar 11, 2015 at 11:11 PM 0
Share

What your asking doesn't really make sense. What does "no longer being called" mean?

Not called last update? Functions don't have "recently used" timestamps, so you have to make one. Won't be called during the next update? How did your program decide that?

It's like asking how to check you aren't having meatloaf for dinner. There's no built-in way. But, if your have a meal list on the Fridge, check what it says for tonight.

It often helps to forget about functions, blah, and thing about what you want to happen in the game.

avatar image
3

Answer by Cherno · Mar 11, 2015 at 11:18 PM

You can set a bool to true in the function, and then in LateUpdate() set it to false, and in Update() you can check if the bool is true or false.

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 DiegoSLTS · Mar 12, 2015 at 12:14 AM

I think I understand what you want.

From the code you shared I guess you are detecting when the "Up" key is pressed and you set that goUp variable to "true". You keep the button pressed and your object keeps moving up, but when you release the Up key, it keeps going up. So, you want a way to detect when the up key is released, am I right?

If you managed to detect a key being pressed you should be able to do something similar to detect when a key is released. For example, if you used the Input.GetKeyDown method to check if a key was pressed, you can use Input.GetKeyUp to check if it was released. It could be done in different ways, if you share the actual code that detects the key being pressed it would be easier to help you with the release event.

When you detect a release event you have to call a different function that sets goUp to false.

Just to be clear, when you hold a the Up key down the function is not "being called" during the whole time you held it down, it's called one time that last a few milliseconds. Functions are not "being called", functions are called and they finish, and can be called again and again.

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

23 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

Related Questions

call function with same name + number 1 Answer

How to make on/off-like gui-button? 3 Answers

How can I write: if this method (X) is activated by another specific method (Y) do this (z) 1 Answer

How can I stop executing a Function in a void Function? 1 Answer

What is the best way to call a function from another script 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