• 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 k3ch0ng · Nov 11, 2014 at 05:32 PM · coroutinenullreferenceexceptionfunctions

NullReferenceException in coroutine when using a function

Anyone know what I’m doing wrong with this coroutine ?

This works fine :

  StartCoroutine( cm.GetChart(strFlag) );

but this call that calls the same function, but in the class gives back a NullReferenceException

  cm.CountryChange(strFlag);



This is in cm Class

 public void CountryChange(string strFlag)
 {
     StartCoroutine( GetChart(strFlag) );
 }    
 
 public IEnumerator GetChart(string strCountry)
 {
 ......
 }

Comment
Add comment · Show 6
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 Yokimato · Nov 11, 2014 at 05:53 PM 0
Share

Can you post the full error? And also the lines of the function listed in the error

avatar image Jeff-Kesselman · Nov 11, 2014 at 10:30 PM 0
Share

Yes we need to see the full exception stack trace.

avatar image k3ch0ng · Nov 11, 2014 at 10:46 PM 0
Share

NullReferenceException UnityEngine.$$anonymous$$onoBehaviour.StartCoroutine (IEnumerator routine) Chart$$anonymous$$anagers.CountryChange (System.String strFlag) (at Assets/_src/Chart$$anonymous$$anagers.cs:8) Test.Start () (at Assets/_src/Test.cs:13)

using UnityEngine; using System.Collections;

public class Test : $$anonymous$$onoBehaviour {

 // Use this for initialization
 void Start () 
 {
     Chart$$anonymous$$anagers cm = new Chart$$anonymous$$anagers();
     
     //StartCoroutine( cm.GetChart("gb") );
     
     cm.CountryChange("gb");
     
     Debug.Log("Done");
 }
 

}

using UnityEngine; using System.Collections;

public class Chart$$anonymous$$anagers : $$anonymous$$onoBehaviour {

 public void CountryChange(string strFlag)
 {
     StartCoroutine( GetChart(strFlag) );
 }    
 
 public IEnumerator GetChart(string strCountry)
 {
     string url = "https://itunes.apple.com/gb/rss/topsongs/limit=10/json";
     
     WWW www = new WWW(url);
     yield return www;

     Debug.Log(www.text);
 }

}

Heres all the code and error :)

avatar image k3ch0ng · Nov 11, 2014 at 11:04 PM 0
Share

This is just code that I quickly mocked up to test and make sure it wasn't something else. All the above code can be pasted into unity to demonstrate the error :)

avatar image Jeff-Kesselman · Nov 11, 2014 at 11:06 PM 0
Share

Your problem is that you are trying to create a $$anonymous$$onoBehaviour using "new". This is expressly forbidden.

A monobehaviour $$anonymous$$UST be owned by a GameObject to make sense.

You need to either add it in the i spector and use GetComponent OR use gameObject.AddComponent()

Either way do NOT use "new"

avatar image Jeff-Kesselman · Nov 11, 2014 at 11:10 PM 0
Share

Well what I posted is the problem with the code you showed us.

If you arent showing us the actual code thats generating the error inside of Unity then there is no way for us to help you\ and you are wasting our time.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Jeff-Kesselman · Nov 11, 2014 at 11:06 PM

Your problem is that you are trying to create a MonoBehaviour using "new". This is expressly forbidden.

A monobehaviour MUST be owned by a GameObject to make sense.

You need to either add it in the inspector and use

 GetComponent<ChartManager>()

OR use

 gameObject.AddComponent<ChartManager>()

Either way do NOT use "new"

Comment
Add comment · Show 7 · 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 Bunny83 · Nov 11, 2014 at 11:10 PM 0
Share

I almost had a feeling like this ^^ But without the actual code it was just "guesswork".

avatar image k3ch0ng · Nov 11, 2014 at 11:15 PM 0
Share

.... The Test.class is already attached to a gameObject, thats where Start() is called to start it all off..... or are you talking about something else?

avatar image Jeff-Kesselman · Nov 11, 2014 at 11:17 PM 0
Share

What class is in the angle brackets?

is it called "Test"?

Only a monobehaviour created properly on a game object can call StartCoroutine or many other $$anonymous$$onoBehaviour functions

avatar image Jeff-Kesselman · Nov 11, 2014 at 11:19 PM 0
Share

And yeah Bunny. Without code at best all we can do is wildly guess.

avatar image k3ch0ng · Nov 11, 2014 at 11:39 PM 0
Share

Guys is the code not showing up in the thread ? I posted a working example half hour ago....

....anyways I have it working now:

I changed

 Chart$$anonymous$$anagers cm = new Chart$$anonymous$$anagers();

To

 this.gameObject.AddComponent<Chart$$anonymous$$anagers>();
 Chart$$anonymous$$anagers cm = this.GetComponent<Chart$$anonymous$$anagers>();

Thanks Jeff :)

and now cm.CountryChange("gb"); seems to works fine.

Just a bit weird that StartCoroutine( cm.GetChart("gb") ); worked fine but not cm.CountryChange("gb"); before.

Show more comments
avatar image
0

Answer by spiceboy9994 · Nov 11, 2014 at 09:25 PM

What's cm? Is that being instantiated before?, I believe that the null reference should be because the method is not static and it requires an instance of a class. If the call and the method are within the same class... you just need to call CountryChange(strFlag) without the "cm." before

Comment
Add comment · Show 1 · 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 k3ch0ng · Nov 11, 2014 at 10:32 PM 0
Share

Yes, cm has already been instantiated.

StartCoroutine( cm.GetChart(strFlag) ); - works fine

cm.CountryChange(strFlag); - NullReferenceException

avatar image
0

Answer by ll3v3ll · Oct 31, 2019 at 05:52 AM

Yes! Thank you. The GameObject (Context) on which the CoRoutine is running must still EXIST! I added a DontDestroyOnLoad to the GameObject to solve this. (I actually was actually using the CoRoutine on a Singleton)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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: Object reference not set to an instance of an object 3 Answers

NullReference exception after StopCoroutine 2 Answers

how can I add the delay between different function calls in C#.. 3 Answers

Calling a random function in a Coroutine 1 Answer

Object reference to set to instance on an instantiated object 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