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 /
avatar image
0
Question by Dylan Cristy · Jul 01, 2011 at 12:57 AM · c#coroutinestaticcs0120

"Object reference required" error when using Start/StopCoroutine

Hi,

I am trying to learn some C#, and to that end I made this script to practice implementing a few things (properties, events, general C# syntax).

The script:

 using UnityEngine;
 using System.Collections;
 
 public class Timer : MonoBehaviour
 {
     private static float timeLeft;
     
     private static bool isTiming = false;
     
     public static float TimeLeft
     {
         get { return timeLeft; }
         set { timeLeft = value; }
     }
     
     public delegate void CountdownEndedHandler ();
     
     public static event CountdownEndedHandler CountdownEnded;
     
     public static void CountDownFrom (float countdownTime)
     {
         if (isTiming) return;
         else
         {
             Debug.Log("CountDownFrom " + countdownTime);
             isTiming = true;
             timeLeft = countdownTime;
             StartCoroutine(Countdown(countdownTime));
         }
     }
     
     private static IEnumerator Countdown (float countdownTime)
     {
         Debug.Log("Firing Countdown()");
         if (timeLeft <= 0) EndCountdown();
         else
         {
             yield return new WaitForFixedUpdate();
             timeLeft -= Time.deltaTime;
             Debug.Log("timeleft = " + timeLeft);
         }
     }
 
     private static void EndCountdown ()
     {
         StopAllCoroutines();
         isTiming = false;
         timeLeft = 0f;
         if (CountdownEnded != null) CountdownEnded();
     }
 }


However, I am getting the following errors:

 Assets/Scripts/Timer.cs(28,25): error CS0120: An object reference is required to access non-static member `UnityEngine.MonoBehaviour.StartCoroutine(System.Collections.IEnumerator)'
 Assets/Scripts/Timer.cs(46,17): error CS0120: An object reference is required to access non-static member `UnityEngine.MonoBehaviour.StopAllCoroutines()'



Part of my objective in this exercise is to create a class that has methods that can be called from other scripts, without this particular class having any instance of itself (i.e., the script lives only in my 'Scripts' folder, and is not attached to any GO), which is why everything is set to 'static'. (That's my understanding of what 'static' does -- makes things available on a more global level as opposed to from an individual instance of something.)

Can anyone help me understand what I'm doing wrong / missing here?

Thanks!

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

Answer by Peter G · Jul 01, 2011 at 01:53 AM

I believe you need an instance to call StartCoroutine() given that StartCoroutine() is an instance method and a static method doesn't have an instance. Normally you can solve that issue with a singleton:

 public class Timer : MonoBehaviour {
    private static Timer instance;
    public static Timer Instance {
        get {
           if(instance == null) {
              var gB = new GameObject("Timer");
              instance = gB.AddComponent<Timer>();
           }
           return instance;
        }
     }
 }

then you just use Instance that you can call all the methods from. You might need to rework some code, but it isn't to hard to fix:

 public void Update() {
     Timer.Instance.StartCoroutine("Time");
 }

And a small note on something else you said. static doesn't make a field global because a private static var is also legal. It is a little different than C/C++, but the static keyword associates the field with the class, not its instances. I think you understand the concept, but there is a difference that sometimes confuses users.

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 Dylan Cristy · Jul 01, 2011 at 05:15 PM 0
Share

Thanks... in the end I did not make the "Timer" class a singleton, but I did have to make another class of which I make an instance in order to run the countdown coroutine...

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Couroutines not working in static class 1 Answer

Where is the Mistake in my Coin System? 1 Answer

Why isn't my simple coroutine working? (and how can I make it infinite?) 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