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 Nsingh13 · Jun 04, 2015 at 06:26 AM · animation2dbugcoroutineienumerator

Line of code skips randomly at times?

So the whole script is pretty long but basically I have an object which when you tap it calls the currency method:

 if (Input.touchCount >= 1) {
 
                         hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint ((Input.GetTouch (0).position)), Vector2.up);
 
                         //On touch
                         if (hit.collider != null && hit.collider.tag == "bug" && Input.GetTouch (0).phase == TouchPhase.Began) {
             
                                 Currency();
                                 hit.transform.gameObject.SetActive (false);
                                 changesprite ();
                                 spritetransform = true;
                                 
                         }


This is my code inside my currency method:

 if(isapple == true)
             {
                 A1.GetComponent<SpriteRenderer>().sprite = apple;
             }
 
             if(iscoin == true)
             {
                 A1.GetComponent<SpriteRenderer>().sprite = coin;
             }
 
             C1.transform.position = new Vector2(hit.transform.position.x, hit.transform.position.y);
         
             StartCoroutine(C1handler());
 
             return;


The problem is inside my coroutine:

     public IEnumerator C1handler()
     {
         C.SetBool("startABT", true);
         C1avail = true;
 
         yield return new WaitForSeconds (0.1f);
 
         Debug.Log("time exceeded");
         C.SetBool("startABT", false);
         C1avail = true;
 }

Most of the time the game randomly skips calling this line:

 C.SetBool("startABT", false);


Anyone know what the problem could be? Any ideas/answers are greatly appreciated. Thanks! :)

Comment
Add comment · Show 2
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 Saad_Khan · Jun 04, 2015 at 08:57 AM 1
Share

How do you know it skips calling the line , have you checked with Debug.Log above the "WaitForSeconds". Are you sure its not because of the fact that you reset the value of "startABT" after just 0.1 seconds

avatar image Nsingh13 · Jun 04, 2015 at 01:56 PM 0
Share

no, I'm ssure it skips because I look at the animator while I test on unity remote. Also it still skips even if I change it to 1 or 2 secs after (my main goal is to have it at 1 sec though)

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by DiegoSLTS · Jun 04, 2015 at 02:09 PM

There's not way a line of code could be "skipped randomly" unless you put that line inside some random condition that might skip it.

Since you're working with coroutines note that deactivating or destroying the object that started the coroutine will stop it (you can't start coroutines on disabled objects for the same reason). Maybe the code it's not skipping that line, maybe the coroutine has stopped completelly, or the next line is always executed?

Also, the line might be called, but another line in another script might set it to true again in the same frame.

Now, I see you have a debug line that prints "time exceeded", that line is always printed? What if you add another debug line below that line. If only the first line is called and not always then your coroutine is being stopped, if both lines are printed then the line between them has been called and the problem is somewhere else (like setting it to true in another script).

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 Nsingh13 · Jun 04, 2015 at 05:43 PM 0
Share

Hi Diego, Thanks for the informative answer, I will investigate when I return home(I'm at school right now). I just realized that the problem is probably due to the fact that I disable my object after calling the currency method. So, like you said, the coroutine is probably being interrupted.

avatar image Nsingh13 · Jun 08, 2015 at 12:43 AM 0
Share

Still couldn't find a solution. I even put the whole 'turn object false' piece of code at the end of the coroutine, but the result is the same.

avatar image EmulatorsGaming · Mar 06, 2019 at 12:31 AM 0
Share
  public void CreateInternet$$anonymous$$atch()
     {       
         if(Password == null || Password == "")
         {
             if(roomName != null || roomName != "")
             {
         Network$$anonymous$$anager.singleton.match$$anonymous$$aker.Create$$anonymous$$atch(roomName,4, true,"", "", "", 0, 0, OnInternet$$anonymous$$atchCreate);
         Debug.Log(roomName+ " public");
             }
 
             Status1.text = "Please enter a name for your room";
 
 
         }
         else
         {
             if (roomName != null || roomName != "")
             {
 Network$$anonymous$$anager.singleton.match$$anonymous$$aker.Create$$anonymous$$atch(roomName,4 , true,Password, "", "", 0, 0, OnInternet$$anonymous$$atchCreate);
                 Debug.Log(roomName + " Private");
             }
             
                 
              Status1.text = "Please enter a name for your room";
         
         }
        
         
     }

than explain why Status1.text is not changing and yes I attached it to the right gameobject

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

22 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

Related Questions

making a 2D character Speak for different lenghts of time 2 Answers

How to prevent holding down a key to infinitely loop an action? 1 Answer

Yield Wait for Seconds (A little help here) 2 Answers

AnimationCurve as fixed time rather than portion of time? 1 Answer

Mecanim animation, playing once 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