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 liortal · Sep 08, 2014 at 06:30 AM · c#coroutinenullreferenceexceptioncoroutines

NullReferenceException in StartCoroutine

I am getting crash reports from players (using an automated error reporting system), although i cannot seem to reproduce this issue.

For some reason, a certain piece of code that starts a coroutine throws a null reference exception:

 StartCoroutine(LoadLevelDelayed());

 // This method is defined properly, returning an IEnumerator and yielding at certain points.
 private IEnumerator LoadLevelDelayed()
 {
    // ...
 }

This script is attached to a game object that is saved inside a scene (i've seen other posts where people tried to manually instantiate MonoBehaviour objects and that was the cause of failure).

Other information that may be related is that the call to StartCoroutine occurs inside an anonymous delegate, e.g:

 public class Something : MonoBehaviour
 {
     public void SomeMethod()
     {
         CallAnotherMethod(
                  delegate(bool success)
                   {
                           StartCoroutine(LoadLevelDelayed());
                   });
     }
 
     private IEnumerator LoadLevelDelayed()
     {
           // ..
     }
 }

What else can lead to a NRE thrown when calling this method (StartCoroutine) ?

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

1 Reply

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

Answer by hangemhigh · Sep 08, 2014 at 08:37 AM

First, remove private from IEnumerator LoadLevelDelayed(); Try that... Where are you calling startCoroutine() from?

EDIT: That is the issue. I've been using coroutine and they worked very well. You can just call the startCouroutine function when your download is done. Coroutine runs at the same time with other codes

so just start coroutine after the download is done. Don't expect Unity to support every feature of C#. Just try what I just said. You will see what I am talking about. You don't need that get coroutine working.

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 liortal · Sep 08, 2014 at 08:42 AM 0
Share

It is a private method, there's no need to expose it outside of this class. I am calling it from the same class (like my example) so the private makes no difference.

avatar image hangemhigh · Sep 08, 2014 at 09:03 AM 0
Share

Removing private doesn't not expose it to outside world. The default is not public. The default is private. Forget about the private thing I said. I think that the delegate stuff might be the problem here.

This way you call the function

  public class Something : $$anonymous$$onoBehaviour
     {
     void Update(){
     Some$$anonymous$$ethod();
    }
     public void Some$$anonymous$$ethod()
     {
     StartCoroutine(LoadLevelDelayed());
     }
      
     private IEnumerator LoadLevelDelayed()
     {
     // ..
     }
     }

When do you want to load level? What is

 CallAnother$$anonymous$$ethod(
 delegate(bool success)
 {
 StartCoroutine(LoadLevelDelayed());
 });

suppose to do and when is it suppose to be called? I don't know what calls the CallAnother$$anonymous$$ethod function but the code I modified should work.

avatar image liortal · Sep 08, 2014 at 09:47 AM 0
Share

We are wrapping this code as a continuation (System.Action) and pass that down to some other code that eventually will call it (after downloading some stuff, it will invoke it). Why might that be an issue? i would look into that.

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

23 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

Related Questions

Waiting twice inside coroutine (C#) 2 Answers

What is way to flash the screen WITHOUT using a Texture? 2 Answers

IEnumerator coroutines crashing app when launched with datatyle "GameObject" passed in 2 Answers

Running a coroutine without StartCoroutine 1 Answer

Toggling bools automatically using coroutines 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