Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
7
Question by CornDog · Jul 11, 2014 at 12:38 AM · coroutinewaitforseconds

NullReferenceException on StartCoroutine()

when run my game at some point it calls Act() and I get the following error:

 NullReferenceException
 UnityEngine.MonoBehaviour.StartCoroutine (IEnumerator routine) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineMonoBehaviour.cs:62)
 Wait.Act () (at Assets/Scripts/Actors/Actions/Wait.cs:7)
 Actor.NextAction () (at Assets/Scripts/Actors/Actor.cs:43)

Unity tells me the NullReference is on the call to StartCoroutine()

The "Action" class does inherit MonoBehavior

The Code:

 using UnityEngine;
 using System.Collections;
 
 public class Wait : Action {
 
     public override void Act () {
         StartCoroutine(WaitAction());
     }
     
     IEnumerator WaitAction() {
         yield return new WaitForSeconds(3);
         Finished();
     }
 }

I have Googled the internet to pieces, and read every bit of documentation I could find to understand what I'm doing wrong. So what have I missed?

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

Answer by Bunny83 · Jul 11, 2014 at 02:05 AM

Well, i just guess that the script (and / or the gameobject it's attached to) has been destroyed, or you created the Wait-instance with "new" which would result in the same behaviour since a MonoBehaviour can't live on it's own.

In C# / .NET / Mono instances actually can't be destroyed since they live in a managed memory environment. Objects are destroyed when all references to the object are gone, no longer valid. After that the garbage collector eventually kicks in and removes the object.

However in Unity, since it's core is written in C++, (native) objects can be destroyed on command (with Destroy to be more precise). The Destroy method actually only destroys the object on the c++ side. The managed representation of the object (your MonoBehaviour script) will still be there since the GC can only collect the object when there are no references anymore. That's why Unity actually "fakes" that the reference is null when the object has lost it's native counterpart.

If you use the "new" keyword to create an instance of a MonoBehaviour derived class the instance doesn't have a native counterpart and will always pretend to be null. If you want to create an instance at runtime it has to be attached to a gameobject. This is done with AddComponent

Comment
Add comment · Show 4 · 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 CornDog · Jul 11, 2014 at 08:20 PM 1
Share

Yep, the problem was that the object was not added as a component of something in the scene. I had to tweak things kind of weird to make it work but now it does. thanks.

avatar image ComeSweetDeath · Feb 17, 2015 at 04:49 PM 0
Share

Thanks for the great explaination. I just have a small question about the ter$$anonymous$$ology: by "objects" do you mean the gameobjects in the hierarchy, and by "instances" do you mean something like an instance of a class created by the "new" keyword?

avatar image Bunny83 · Feb 18, 2015 at 01:50 AM 1
Share

@ComeSweetDeath: An object is an instance of a class ^^ So it actually means the same. The confusing thing which gets most people is, that there are two "worlds" where objects can live: The native C++ world (the actual, underlaying engine) and the managed environment which is used for scripting (C# / UnityScript / Boo).

In both cases "object" or "instance of a class" means the same. An object / instance is actually only made up by the data it contains. All methods belong to the class. So an instance of a class is the "thing" that sits somewhere in your memory and represents the data of that instance / object.

avatar image neuroshock · Oct 16, 2017 at 04:20 AM 0
Share

This helped me out so much. THAN$$anonymous$$ YOU!!!!! The headache is FINALLY over!

$$anonymous$$y mistake was i as making a new reference to the unity class that had the erroring Coroutine in it rather than having the class as a component.

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

27 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 avatar image avatar image avatar image avatar image

Related Questions

How to make loop with call to IEnumerator actually pause? 2 Answers

WaitForSeconds Not Working 4 Answers

Problem with coroutine 2 Answers

How to implement a "wait" after an animation has finished? 2 Answers

What is the best method for setting up a variable wait time in a coroutine? 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