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
2
Question by avantius · May 09, 2013 at 04:42 AM · coroutinefunctionclassyield

How to use yield within a class function

Im having an issue trying to implement yield within class functions, what im trying to do, is create classes that will contain the behavior of the player/enemies.

Here's an example of what i'd like to do:

 class Body{
 private var isStunned:boolean;
 
 function Body(){
 <constructor data>
 }
 
 function getStunned(stunTime:int)
 {
 isStunned=true;
 yield WaitForSeconds(stunTime);
 isStunned=false;
 }
 
 }

I've checked around looking for other questions, and what they seem to suggest is to call these functions from within a MonoBehavior script, but that doesn't let me achieve what I want to be able to do. Basically, i'm trying to make a base class that has basic functions that every enemy/player will have and then create aditional classes that extend from it for each.

 class imAnEnemy{
 private var myBody:body;
 
 function imAnEnemy()
 {myBody=new body()}
 
 function isActive()
 {<call other functions from body, which may include yield>}
 
 }

From the example code above, i'd call the isActive() function in the Update Method, but sadly I havent found a way to make this work, is there any elegant alternatives to what I want to do? I know I could just brute-force everything into a MonoBehavior script, but to me that would seem to add too much redundancy.

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 Fattie · May 09, 2013 at 05:33 AM 0
Share

Do these discussions help?

http://answers.unity3d.com/questions/433171/can-a-monobehaviour-extend-a-class.html

http://answers.unity3d.com/questions/430582/extending-a-singleton-base-class-ujs-.html

http://answers.unity3d.com/questions/342307/whats-the-right-way-to-deal-with-inheritance-and-c.html

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by whydoidoit · May 09, 2013 at 06:40 AM

You have to start them as coroutines - normally Unity does that if you call it from within a MonoBehaviour. If you want a utility class then you are going to need a utility MonoBehaviour script to run the coroutines on. In other words you need a script that's purpose in life it to execute your coroutines from your library.

It could be as simple as:

CoroutineSupport.js

     static var instance : CoroutineSupport = this;

     function RunCoroutine(coroutine : IEnumerator)
     {
           return StartCoroutine(coroutine);
     }

Then to start a yielding library routine you would use:

   CouroutineSupport.instance.StartCoroutine(someCoroutineFunction());
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 avantius · May 10, 2013 at 12:30 AM 0
Share

Hmmm, I just tried to do that, created a script pretty much exactly the same as you did (the CoroutineSupport.js), and attempted to call a function using CouroutineSupport.instance.StartCoroutine(someCoroutineFunction()); , but Unity gives me this error: BCE0020: An instance of type 'CoroutineSupport' is required to access non static member 'instance'.

avatar image whydoidoit · May 10, 2013 at 06:29 AM 0
Share

Sorry should have been static var instance.

avatar image Fattie · May 10, 2013 at 06:56 AM 0
Share

@whydo, have a look at this ...

http://answers.unity3d.com/questions/17916/singletons-with-coroutines.html#answer-449919

(search to my answer which mentions "grid")

After years of struggle with this, I have found that to be the most workable real-life solution. Have you ever taken this approach? Can you see any gotchyas? What do you think?

It makes one's code look beautiful, well to me. All our code is riddled with "grid." ...

 if ( weaponNumberIfPowerup == 0 )
     {
     grid.gamecenterstuff.whatever();
     grid.funnyvoices.whatever();
     multiplierRunOver( 2, generalPosition.position );
     }

For me it just makes a lot of sense. it's not fighting Unity. Even in a way the "grid" Class (as in the link) becomes a handy kind of self-documenting "registry" if you will of those sorts of things, you can even look at it that way.

I dunno. I get really troubled by this issue.

avatar image
0

Answer by avantius · May 10, 2013 at 02:08 AM

I searched some more and found a solution for my issue. In case its of use to anyone else, here's the example code of what I found on the Unity forums:

 // Coroutiner.js
 // this class is just used to run coroutines and needs 
 // no further functionality
 class Coroutiner extends MonoBehaviour
 {
 }
 
 // Test.js
 // a class inheriting from System.Object, using 
 // coroutines:
 
 class Test extends System.Object
 {
 public static var coroutineRunner : Coroutiner = null;
 function Test()
         {
         if (coroutineRunner == null)
             {
             var go = new GameObject("Coroutiner");
             coroutineRunner = go.AddComponent(Coroutiner);      
             }       
         }
 function Go()
         {
         coroutineRunner.StartCoroutine(MyCoroutine());
         }
 function MyCoroutine()
         {
         yield;
         Debug.Log("it worked!");
         }   
 }
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 Fattie · May 10, 2013 at 07:06 AM 0
Share

Oh brother.

I am so wary of automatically making a game object.

You know it's true, a couple yrs ago in the endless search for "singleton coroutines" ... see the links I posted .. someone came up with this idea "hey, this code "automatically" does coroutines" ... i.e., the trick is it adds a gameObject.

I just dunno. It's such a disturbing idea.

{One point, if I had code to automatically add game objects man it would have to be robust. What about the na$$anonymous$$g issue, uniqueness, other routines you have that look through gameobjects for some reason, what if this starts running over and over, etc etc. I mean where does it add this coroutiner game object? to the current object, on the top of the scene, what? should this new object be dontdestryonload? all these issues are tricky and would need to be thought-out. my feeling on program$$anonymous$$s is anything that isn't instantly totally inevitably obvious and needs no thought -- is no good! heh}

I am definitely not a "language mechanic", I just want an expert to fix such issues for me. So I don't chime in much on such debates. But for me it's just very shoddy/scary this idea that you can automatically add a game object and that "solves" the singleton-coroutine problem.

I mean conceptually in the broad sense, there are things in your project that are coroutines, versus those that are not coroutines ....... it's a huge, tremendous gulf, they are really different sorts of "things". I'm troubled by trying to in a sense "make them all work together".

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

15 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

Related Questions

How to properly use Yield for a coroutine? 3 Answers

StartCoroutine important for using yield? 1 Answer

Null Reference Exception after updating from 4.0 to 4.1.5 (coroutines) 0 Answers

Coroutoutine doesnt work properly. 3 Answers

Im a bit confused on a simple script 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