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
-1
Question by SC4V4NGER · Apr 08, 2014 at 06:55 PM · beginnerif-statementssuggestions

prevent if() statement from excecuteing SOMETHING multiple times?

Hello,

I want the following script to Send the "Something" just once:

     IEnumerator MakeItWork() {
 
         if(activeContact && !IGNORE)
         {
             yield return new WaitForSeconds(reactionTime);
             Debug.Log ("Something"); //I understand that you can only see this in the Console
             IGNORE = true;
         }
 
         if(IGNORE)
         {
             yield return new WaitForSeconds(1f);
             IGNORE = false;
         }
 
     }

If it would work like i'd want it to do, it should send the message just once... but ut isn't. (edit: it is sending it multiple times in a row)

Any suggestions what to use instead for achieving what I want?

I am still kind of new to Unity and learning, so thanks :)

Comment
Add comment · Show 5
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 perchik · Apr 08, 2014 at 07:04 PM 0
Share

What is it doing? Does it print "Something" continuously? Does it print it every second?

avatar image SC4V4NGER · Apr 08, 2014 at 07:20 PM 0
Share

The Debug.Log is just to make it easyer to explain what I want to achieve. In this case it sends "Something" like 20 times in a row, after that it doesn't send anything. But I only want it to send the message once, and afterwards nothing.

avatar image ffxz7ff · Apr 08, 2014 at 07:31 PM 0
Share

Are you calling $$anonymous$$akeItWork in Update() or so?

avatar image ffxz7ff · Apr 08, 2014 at 07:32 PM 0
Share

If yes, I assume it gets called 20 times before the Coroutine finishes for the first time to set IGNORE to true.

avatar image perchik · Apr 08, 2014 at 07:51 PM 0
Share

what if you set ignore to true before you yield out?

3 Replies

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

Answer by SC4V4NGER · Apr 09, 2014 at 08:50 AM

I fixed it:

 void Start () {
         StartCoroutine( MakeItWork() );
     }
 IEnumerator MakeItWork () {
 
 do{
             yield return new WaitForSeconds(reactionTime);
             Debug.Log("Something");
             IGNORE = true;
             StartCoroutine(MakeItWorkTwo());
         }while(activeContact && !IGNORE);
 }
 
 IEnumerator MakeItWorkTwo() {
 
         do
         {
             yield return new WaitForSeconds(playerHealth.invincibilityTime); 
             IGNORE = false;
             StartCoroutine(MakeCanKill());
         }while(IGNORE);
 
     }
 
 
 Thanks to ffxz7ff for pointing out the problems occuring when calling the function in Update :)

Maybe this helps someone someday

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 ivomarel · Apr 08, 2014 at 07:00 PM

Make sure you call the MakeItWork() method with StartCoroutine(MakeItWork()) and not just like a regular method.

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 SC4V4NGER · Apr 08, 2014 at 07:18 PM 0
Share

yes I know that :) but thanks

avatar image
0

Answer by saschandroid · Apr 09, 2014 at 07:31 AM

Don't use

 if(IGNORE)
 {
     yield return new WaitForSeconds(1f);
      IGNORE = false;
 }

just

 else
 {
      yield return new WaitForSeconds(1f);
 }

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 SC4V4NGER · Apr 09, 2014 at 08:13 AM 0
Share

I can't because that is where IGNORE is set back to false. This way it would just stay true

avatar image SC4V4NGER · Apr 09, 2014 at 08:17 AM 0
Share

But I already fixed it :) I submitted a answer but somehow it isn'T there anymore...

 void Start () { 
  StartCoroutine( $$anonymous$$akeItWork() ); 
 } 
 
 IEnumerator $$anonymous$$akeItWork () { 
  do{ 
  yield return new WaitForSeconds(reactionTime);    Debug.Log("Something"); 
 IGNORE = true; 
 StartCoroutine($$anonymous$$akeItWorkTwo()); 
 }while(activeContact && !IGNORE); 
 } 
 
 IEnumerator $$anonymous$$akeItWorkTwo() { 

do { yield return new WaitForSeconds(playerHealth.invincibilityTime); IGNORE = false; StartCoroutine($$anonymous$$akeCan$$anonymous$$ill()); }while(IGNORE); }

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

26 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

Related Questions

Multiple Cars not working 1 Answer

Getting Started, know my goal but what's a good first-game for me to Create? 2 Answers

Help with Basic Movement Script 1 Answer

How do i make my object jump so when it reaches hte start position it stops falling, at the moment it just keeps falling down the screen and doesn't stop. 3 Answers

Object will not instantiate 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