- Home /
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 :)
What is it doing? Does it print "Something" continuously? Does it print it every second?
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.
Are you calling $$anonymous$$akeItWork in Update() or so?
If yes, I assume it gets called 20 times before the Coroutine finishes for the first time to set IGNORE to true.
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
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.
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);
 }
I can't because that is where IGNORE is set back to false. This way it would just stay true
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
 
 
             Follow this Question
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
Object will not instantiate 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                