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 schwertfisch · Mar 09, 2011 at 07:02 PM · functionbooleaninvokerepeatingwhile

Call a function repeatedly while a boolean is true

I have a variable: boolean, that under certain conditions turns from false (default value) to true for a limited time and then automatically it goes back to false.

While it's true (eg for 5''), I want a function to be called repeatedly (this function makes an SM2 sprite appear and disappear fast, as an eye candy)

Using the following lines, doesn't work:

function Start (){
    while (myBoolean == true){
    InvokeRepeating("MyEyeCandyFunction",0,0.4);
    }
}

What am I doing wrong? Thanks :-)

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
3
Best Answer

Answer by Mike 3 · Mar 09, 2011 at 08:15 PM

Just return from your eye candy function if the boolean is false

function Start (){ InvokeRepeating("MyEyeCandyFunction",0,0.4); }

function MyEyeCandyFunction() { if(!myBoolean) { return; }

 //your code

}

If you want it to stop invoking it when the boolean has turned false:

function Start (){ InvokeRepeating("MyEyeCandyFunction",0,0.4); }

function MyEyeCandyFunction() { if(!myBoolean) { CancelInvoke("MyEyeCandyFunction"); return; }

 //your code

}

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 schwertfisch · Mar 12, 2011 at 02:01 PM 0
Share

Thanks so much $$anonymous$$ike. I had no idea I could use "return". Where can I read about it's use? (Searching the script reference I did not find anything).

avatar image Mike 3 · Mar 12, 2011 at 10:54 PM 0
Share

$$anonymous$$ost program$$anonymous$$g sites/books - it's a general concept. It basically just returns the execution back to the function that called it (in this case, something in the unity engine), though you can use it to return an object back too (That's how nearly all of the unity functions give you data back)

avatar image schwertfisch · Mar 13, 2011 at 03:09 PM 0
Share

This helped!___

avatar image
1

Answer by DaveA · Mar 09, 2011 at 08:07 PM

var lastCheck = 0.0; var checkInterval = .4;

void Update() { if (myBoolean && (Time.time - lastCheck) >= checkInterval) { MyEyeCandyFunction(arguments); lastCheck = Time.time; } }

This will call MyEyeCandyFunction the first time myBoolean goes true, and then every checkInterval seconds thereafter until myBoolean goes false

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 schwertfisch · Mar 12, 2011 at 02:01 PM 0
Share

Thanks very much DaveA, I marked $$anonymous$$ike's answer as accepted, because I found it more simple. Your suggestion gave me some ideas I could use in other occasions though :)

avatar image
0

Answer by Tuck · Mar 09, 2011 at 07:28 PM

Put your code in an if statement inside of Update. It will be called every frame. For example:

void Update()
{
   if(myBoolean)
   {
      MyEyeCandyFunction(arguments);
   }
}

You will need to keep track of how many times your eye candy function is called and then set myBoolean to false after a certain amount of calls. If you don't want this to be called every frame check against Time.time to see how often to call it. I am in a rush but post a comment if you want me to get more in detail.

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 schwertfisch · Mar 09, 2011 at 07:39 PM 0
Share

Thanks Tuck, but this doesn't help me much. "myBoolean" is automatically changed back to false, so I want this change to trigger the ceasing of my eye candy function repeated call.

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

Why is InvokeRepeating not working in boolean method? 0 Answers

How can I call InvokeRepeating wich can be found in the start function, after I Cancelled the Invoke? 1 Answer

How can I use a yeild during an udate? 1 Answer

Check a boolean function without using Update() 1 Answer

on mouse enter without function update 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