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 iarnazca · Jul 05, 2014 at 03:03 AM · coroutinerunonce

coroutine spamming webserver which then stops replying

I have a coroutine. My question is how do I make it only run once? It seems that the routine is repeatedly running so many times that the webserver think's i'm attacking it with a dos attack and stops replying. This breaks my game and causes a crash.

Here is the code.

 void OnGUI () {
 
         if(showMyDecks == true)
         {
 
 
 
 
             //here we need to query a php script to see if the player has any decks.
             //and display those decks with what ever the player has named these decks.
             if (deck1 == null)
             {
                 StartCoroutine("getDeck1");
                 while (deck1 == null){
                 }
                 StopCoroutine("getDeck1");
             }

and the coroutine

 public IEnumerator getDeck1(){
         WWWForm form = new WWWForm ();
         form.AddField ("userId", userId);
         form.AddField ("deck", "1");
         WWW www = new WWW(url, form); 
         yield return www; 
         response = www.text;
         //this should respond with the name of the deck.
         
         deck1 = response ;
         yield return;
     }


any help would be appreciated thank you.

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

2 Replies

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

Answer by NorthernVisionStudio · Jul 05, 2014 at 03:58 AM

OnGUI is called every frame, which is the source of your spamming behavior. That means OnGUI is starting a new coroutine each frame. The simplistic way to 'fix' it is to make a new variable called "busy" in your main Monobehavior class. Then here would be your coroutine:

 public IEnumerator getDeck1(){
 if (!busy)
 {
   busy = true;
   WWWForm form = new WWWForm ();
   form.AddField ("userId", userId);
   form.AddField ("deck", "1");
   WWW www = new WWW(url, form);
   yield return www;
   response = www.text;
   //this should respond with the name of the deck.
  
   deck1 = response ;
   busy = false;
   yield return;
   }

This will run as quickly as the web server returns the data. You may want to insert a delay in the coroutine to tone the frequency down, such as yield return new WaitForSeconds(x)

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 iarnazca · Jul 05, 2014 at 09:16 PM 0
Share

Great answer! I ended up created a variable and in OnGUI() checked for it to be true with an if statement rather than checking in the coroutine.

The real problem was me not realizing OnGUI() was starting the coroutine every single frame.

Thanks again! I must have spent hours in this already lol.

avatar image NorthernVisionStudio · Jul 07, 2014 at 06:18 AM 0
Share

You're welcome. Good idea!

avatar image
0

Answer by musaranya · Jul 05, 2014 at 04:13 AM

I think your coroutine is been called each time OnGUI is executed and until the first call sets a value to deck1. You can try the follow:

  • declare a flag as a class member to prevent calling the coroutine more than once and initialize it to false

  • set this flag to true when entering the coroutine and false when finishing it

  • always ask for the flag value before call the coroutine

    bool getDeck1Flag;

    void Start() { getDeck1Flag=false; }

    void OnGUI () {

           //here we need to query a php script to see if the player has any decks.
             //and display those decks with what ever the player has named these decks.
             if(showMyDecks && !getDeck1Flag && deck1 == null)
                 StartCoroutine("getDeck1");             
     }
     
     public IEnumerator getDeck1(){
             getDeck1Flag=true;
             
             WWWForm form = new WWWForm ();
             form.AddField ("userId", userId);
             form.AddField ("deck", "1");
             WWW www = new WWW(url, form); 
             yield return www; 
             response = www.text;
             //this should respond with the name of the deck.
             deck1 = response ;
             
             getDeck1Flag=false;
             yield return;
     }
    
     
    
    

This is only an approach but I think it could work. From here you need to think what would happen if the server doesn't response and update the code accordingly. Hope it helps!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Animation method/coroutine playable once or loop 0 Answers

Anything like run once? 1 Answer

AI function/coroutine animation issues 2 Answers

StartCoroutine works once 1 Answer

Do I have to tell unity to load assets only once with cSharp script 4 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