• 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 sidvbhave · Jul 26, 2013 at 08:09 AM · coroutinenullreferenceexception

Running a MonoBehavior script A with a coroutine in another Script B

Hi, I am a newbie in OOP and Unity/C#. I am using WWWForm in a script called PostData.cs that posts data on a server. Here is the code for PostData.cs

 using UnityEngine;
 using System.Collections;
 
 public class PostData : MonoBehaviour {
     
     public string SecretKey = "this works!";
     
     void Start ()
     {
         PostData();
         }
     
     public void PostData()
     {
         string url = "http://place/somefile/awesome.php"; 
       
         WWWForm form = new WWWForm();
 
         //form.AddField(,);
         form.AddField("name", abc);
         form.AddField("school", "cdf");
                 form.AddField("rating", 5);
         form.AddField("yes/no", 1);
         form.AddField("description", "goals");
         
         WWW www = new WWW(url, form);
 
         StartCoroutine(WaitForRequest(www));
         
     }
     public string SecretKey ()
     {
         return secretCodeWord;
     }
 
         public IEnumerator WaitForRequest(WWW www)
     {
         yield return www;
         if (www.error == null)
         {
             Debug.Log("WWW Ok!: " + www.text);
         } else {
             Debug.Log("WWW Error: "+ www.error);
         }    
     }    
 }

Now I want to run this script coroutine that writes the data to the server in another script called TestPost.cs. I have tried to write it as follows

 using UnityEngine;
 using System.Collections;
 
 public class TestPost : MonoBehaviour {
     
     // create an instance
     PostData postIt = new PostData();
     
     void Start () {
             Debug.Log ("started");
             // calling the simple function to test
             Debug.Log(postIt.getCodeWord());
             // calling the required function
             postIt.PostData();
             Debug.Log("something happened");
     }
 
 }

When I run the TestPost.cs file I get the following output:

 started
 
 this works!
 
 NullReferenceException
 UnityEngine.MonoBehaviour.StartCoroutine (IEnumerator routine)

So calling the simple string return function works, but the coroutine is not called. Does anyone have an idea of what I am doing wrong? Any kind of help will be appreciated. thanks

PS: Sorry, I ended up asking a very lengthy question!!

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
1
Best Answer

Answer by gregzo · Jul 26, 2013 at 08:17 AM

You cannot instantiate Monobehaviour classes with the keyword new.

Instead, use AddComponent :

 using UnityEngine;
 using System.Collections;
  
 public class TestPost : MonoBehaviour {
  
 // create an instance
 PostData postIt; // no new keyword, just declaring a reference
  
      void Start () {
      Debug.Log ("started");
      postIt = gameObject.AddComponent ( typeof ( PostData ) ) as PostData; //Here!
      // calling the simple function to test
      Debug.Log(postIt.getCodeWord());
      // calling the required function
      postIt.PostData();
      Debug.Log("something happened");
      }
 }
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 sidvbhave · Jul 26, 2013 at 08:26 AM 0
Share

Thanks a lot gregzo..!! You saved my life..!! I was trying to solve this for more than 6 hours...

avatar image gregzo · Jul 26, 2013 at 09:01 AM 0
Share

No trouble, trying to instantiate $$anonymous$$onoBehaviours like a normal object is a common mistake!

avatar image Hector Mendoza · May 27, 2014 at 10:29 PM 0
Share

Thanks guys!!

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

16 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

Related Questions

NullReferenceException in coroutine when using a function 3 Answers

NullReferenceException: Object reference not set to an instance of an object 3 Answers

NullReference exception after StopCoroutine 2 Answers

Object reference to set to instance on an instantiated object 1 Answer

How to start a Coroutine in another Script? 2 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