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 /
  • Help Room /
avatar image
0
Question by Link0n3 · Apr 26, 2016 at 02:56 PM · c#errorcoroutine errorsruntime-error

NullReferenceException on StartCoroutine. Does anyone know what it could cause that?

Hello community,

I am getting a run-time error when stating a Coroutine. The calls inherit from a class that, again, inherit from MonoBehaviour and an interface. The class is instantiated run-time and it is not connected to any gameobject available in the current scene. My sample code

 public class TaskTrialState: MonoBehaviour, AIState
 {
      ...
 }
 public class FollowBeaconTimer : TaskTrialState
 {
 protected override Enter(TaskTrialAI agent)
 {
     base.Enter(agent);
     ...
     SetBeacon();
 }
 private void SetBeacon()
 {
 StartCoroutine(DelayedSetInputState(true,2.5f));
 }
 private IEnumerator DelayedSetInputState(bool state,float delay)
 {
 yield return new WaitForSeconds(delay);
 SetInputState(true);
 }
 }

The error has the following text

NullReferenceExceptionUnityEngine.MonoBehaviour.StartCoroutine (IEnumerator routine) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineMonoBehaviourBindings.gen.cs:6

Any idea what could be wrong? Could it be from the simple fact that the script is not attached to a gameobject? Any hint would be very useful, also to help me with a workaround.

Thanks.

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 Jessespike · Apr 26, 2016 at 05:15 PM 0
Share

$$anonymous$$onoBehaviours should be on a GameObject. Ins$$anonymous$$d of instantiating the class, have you tried making a new GameObject and adding the component?

avatar image Pharaoh_ · Apr 26, 2016 at 06:27 PM 1
Share

Classes deriving from $$anonymous$$onoBehavior should normally be attached on gameobjects - this is why you use the parent class, to utilize the classes as components. I am not sure how you instantiate this class, because in the case of a new() keyword, you should already receive a warning in the Editor, suggesting that you should add it as a component ins$$anonymous$$d.

Finally, I am not sure if this is a copy-paste mistake, but SetInputState(true) has no semicolon in the end. Can I also ask where exactly this method is located, which could be the potential reason for the null exception?

avatar image Link0n3 Pharaoh_ · Apr 27, 2016 at 10:00 PM 0
Share

Edited the missing ; thanks for spotting it out.

I edited bits of the code to make every step more clear (and also

The FollowBeaconTimer is a state-machine state. The "new" is done when changing state. This is the state-machine base class:

 public class State$$anonymous$$achine<T>
     {
         T agent;
         AIState<T> currState = null;
           
         public State$$anonymous$$achine(T _agent)
         {
             agent = _agent;
         }
 
         public void SwitchState(AIState<T> state)
         {
             if (currState == state)
             {
                 Debug.LogWarning("Switching to the same state: " + state.Name);
             }
 
             if (currState != null)
             {
                 currState.Exit(agent);
             }            
             currState = state;
             currState.Enter(agent);
         }
         
         public void AIUpdate()
         {
             if (currState != null)
             {
                 currState.Execute(agent);
             }
         }
 
     }

And another player of all of this is the class (attached to a gameobject) that have has memeber the State$$anonymous$$achine class. In the end I have already solved with a dirty trick. Since I am using coroutines just to delay execution I use the class having the state machine for my coroutine and at the I set a callback to the class the that started it. I am not sure with this structure if I can really solve the problem as I would not like to attach the script to a game object.

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

Inactive in console even though its active in the inspector. 0 Answers

Error CS0246: The type or namespace name 'Condition' could not be found (are you missing a using directive or an assembly reference?) (CS0246) (Assembly-CSharp) 1 Answer

error CS0117 UnityEngine.Physics2D PlayerControl 0 Answers

How to convert an inputfield's string to an int? (C#) 1 Answer

[C#] The left-hand side of an assignment must be a variable, a property or an indexer 0 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