Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 AdamLazaruso · Sep 29, 2015 at 04:05 PM · unity 5coroutinenullreferenceexceptionstopcoroutine

NullReference exception after StopCoroutine

This is a little bit of a mess so I hope I can explain it well enough.

In my code I have a Coroutine called OrganiseChildren which moves around the children of the game object depending on what's going on in game. This function works fine.

However, if the game object hits another object then I'd like there to be a 'grace' period of 3 seconds in which everything stops moving. If the object gets hit again during the grace period, then it should start back at 3 and so on. My strategy for this is to write a separate coroutine (EnemyContactTimer) that, if the player is hit, stops the OrganiseChildren coroutine, waits until the time has passed, and then starts it again.

Here's the relevant code:

 private float endTime;
 IEnumerator organiseCoroutine;

 void Start () {
     endTime = -1;
     organiseCoroutine = OrganiseChildren();
     StartCoroutine(organiseCoroutine);
 }

 void OnTriggerEnter2D(Collider2D other)
     {
             endTime = Time.time + 3;
             StartCoroutine(EnemyContactTimer());
         }
     }

 IEnumerator EnemyContactTimer()
     {
         StopCoroutine(organiseCoroutine);
         while(Time.time < endTime)
         {
             yield return null;
         }
         StartCoroutine(organiseCoroutine);
         yield return null;
     }
 
 protected IEnumerator OrganiseChildren()
         {
             while (true)
             {
                //bunch of other code
                 }
                 yield return null;
             }
         }

The problem is that once the StopCoroutine function is called within EnemyContactTimer() it pulls a NullReferenceException at coroutine stops.

 NullReferenceException: (null)
 UnityEngine.MonoBehaviour.StopCoroutine (IEnumerator routine) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineMonoBehaviourBindings.gen.cs:86)
 BallController+<EnemyContactTimer>c__Iterator1.MoveNext () (at Assets/Scripts/BallController.cs:53)
 UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
 BallController:OnTriggerEnter2D(Collider2D) (at Assets/Scripts/BallController.cs:47)


Line 53 is the StopCoroutine(organiseChildren); line in the EnemyContactTimer function.

I'm completely stumped with this one and don't know why stopping the OrganiseChildren coroutine is causing a null reference in EnemyContactTime.

Any help with this would be much appreciated.

Comment
Add comment · Show 3
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 Toon_Werawat · Sep 29, 2015 at 05:02 PM 0
Share

IEnumerator will throw error if you yield null; But in while block. They not throw any error.

 IEnumerator EnemyContactTimer()

 {

     StopCoroutine(organiseCoroutine);

     while(Time.time < endTime)

     {

         yield return null;

     }

     StartCoroutine(organiseCoroutine);

     yield return null; < Try change this to  - yield break;

 }
avatar image AdamLazaruso Toon_Werawat · Sep 29, 2015 at 05:53 PM 0
Share

Nope, didn't help. Thanks for the effort anyway.

avatar image GlatiatorRX · Sep 29, 2015 at 06:18 PM 0
Share

I always got errors stopping them, so I never really used the StopCoroutine function. I do not know if this is exactly what you want, but here is a way you could just full out avoid stopping the coroutine. Ins$$anonymous$$d basically making it a toggle, so that until the coroutine is complete, do not allow it to be called again.

 private float endTime;
 private bool organizeStarted;
  IEnumerator organiseCoroutine;
  void Start () {
      endTime = -1;
      organizeStarted = true;
      organiseCoroutine = OrganiseChildren();
      StartCoroutine(organiseCoroutine);
  }
  void OnTriggerEnter2D(Collider2D other)
  {
      endTime = Time.time + 3;
      if(!organizeStarted)
      {
         // Do not allow coroutine to be called until finished
         organizeStarted = true;
         StartCoroutine(organiseCoroutine);
      }
  }
  
  protected IEnumerator OrganiseChildren()
  {
     // Wait for allotted time
      while (Time.time < endTime)
      {
         yield return null
      }
         
     //bunch of other code
     
     // Allow coroutine to be called again
     organizeStarted = false;
  }

I hope this helps.

2 Replies

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

Answer by MiniFish · Sep 30, 2015 at 10:19 AM

try this

 IEnumerator EnemyContactTimer()
     {
         StopCoroutine(organiseCoroutine);
         organiseCoroutine = OrganiseChildren(); // reassign the IEnumerator variable
         while(Time.time < endTime)
         {
             yield return null;
         }
         StartCoroutine(organiseCoroutine);
         yield return null;
     }

somehow StopCoroutine() will cause the IEnumerator to clear itself. or rather, from my experiences, i cannot get the IEnumerator to start again if i stopped it.

i also suspect that you'd lose whatever values that is still inside the stopped coroutine when you stop it, so it's more like a restart than a pause and resume.

see the answer posted in this question. it should clear up a fair bit of coroutine magic.

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
0

Answer by Hullu · Sep 30, 2015 at 01:42 PM

Try something like this

    yield return StartCoroutine(EnemyContactTimer());

The OrganiseChildren should pause untill EnemyContactTimer is finished

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

NullReferenceException in StartCoroutine 1 Answer

Coroutine Help. 1 Answer

Why doesnt my coroutine ever end? 2 Answers

Coroutine state control 1 Answer

Has my project's data corrupted? Script keeps returning null but 20 minutes ago it wasn't? 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