Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Reaksmey-Rt · Oct 28, 2014 at 05:12 AM · coroutines

Use Delay time in Abstract Class

Hello !! I am a study that try to create game in unity 3D. I have a problem .my question is "I can't to use: Startcouratince(functionname()); for delay time in abstract : public classname PlayState : IStateBase {} but i only can use in normal class Class name:MonoBehaviour {} "

alt text

capture.png (4.4 kB)
Comment
Add comment · Show 1
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 Reaksmey-Rt · Oct 28, 2014 at 08:20 AM 0
Share

Dear sir/$$anonymous$$adam

Thank you for this answer. But I need to make Delay time in Abstract Class. Do you have other way to make Delay time for using in abstract class?

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by fafase · Oct 28, 2014 at 08:35 AM

Your abstract class can inherits from MonoBehaviour, IStateBase is probably an interface so:

  public abstract class PlayState:MonoBehaviour, IStateBase{}

If IStatebase is also a class then add MonoBehaviour at some point of the inheritance.

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 Reaksmey-Rt · Oct 28, 2014 at 10:50 AM 0
Share

Oh Thank you for this.

avatar image
3

Answer by Bunny83 · Oct 28, 2014 at 05:59 AM

Coroutines run on GameObjects / MonoBehaviours. That's why StartCoroutine is a method of the MonoBehaviour class. You can define coroutines in custom classes, but you have to use a MonoBehaviour component to start (and run) the coroutine.

I've written a singleton class called CoroutineHelper which has some useful methods to run coroutines or even OnGUI code from wherever you want (except other threads ;)).

In your case all you need is the singleton property of the class. So you could simply use a helper like this:

 public class SimpleCoroutine : MonoBehaviour
 {
     private static SimpleCoroutine m_Instance = null;
     public static SimpleCoroutine Instance
     {
         get
         {
             if (m_Instance == null) {
                 m_Instance = (new GameObject("SimpleCoroutine")).AddComponent<SimpleCoroutine>();
                 DontDestroyOnLoad(m_Instance.gameObject);
             }
             return m_Instance;
         }
     }
 }

You don't need to attach this script to any object manually. Just use it like this:

 SimpleCoroutine.Instance.StartCoroutine(YourCoroutine());

That's all.

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 Kiwasi · Oct 28, 2014 at 07:25 AM 1
Share

This works. Under the hood coroutines are not that difficult. You can implement a system to run them yourself by calling IEnumerator.$$anonymous$$oveNext.

avatar image Reaksmey-Rt · Oct 28, 2014 at 08:21 AM 0
Share

Dear sir/$$anonymous$$adam

Thank you for this answer. But I need to make Delay time in Abstract Class. Do you have other way to make Delay time for using in abstract class?

avatar image Bunny83 · Oct 29, 2014 at 05:08 PM 1
Share

@reaksmey-rt: Uhm, as i said this works no matter from what class you execute it. An abstract class is just a base class. From an "abstract class" itself you can't do anything because you can't have an instance of an abstract class. So you probably have a derived class which is not abstract. It doesn't matter if you start the coroutine with my approach from code that is in the abstract class or in the derived class. It will work (again: If you execute this on the main thread as Unity is not thread safe).

You will for sure get no better answer without more details. Again the fact that you have an abstract class somewhere is no reason at all. You provide only general and abstract information, we can only answer with general information. If you want a specific answer, ask a specific question and include the required information.

If you don't want to post your class(es) here that's fine, but then post an equivalent example that shows what you do and where you want to do what.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Brain exploding ... Problem with an internal Coroutine manager 3 Answers

Coroutines stop when another game goes fullscreen 1 Answer

I don't understand coroutines and invoke 1 Answer

Setting SplatPrototypes is very slow! (50 ms every Terrain), coroutines/threading? (I need suggestions) 0 Answers

Move animation in coroutine problem 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