• 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 thaiscorpion · Nov 26, 2013 at 02:42 PM · javascriptcoroutineclass

How to use corutines in a class

Hi, I'm trying to make a menu class with a fade function. My problem is that I can't used coroutines in my class, they just don't work and I don't get any errors in the console. I want to be able to write something like this:

myMenu.FadeOut();

or

yield myMenu.FadeOut();

I'm not sure, I've tried every combination I can think of but nothing.

this is my class:

 static class Menu {
 
     var menu //this is assigned in another part of the class
     
     function FadeOut(){
         while (menu.renderer.material.color.a > 0){
             menu.renderer.material.color.a -= 0.1;
             yield WaitForSeconds(1);
         }
     }
     
     //I have removed the rest of the functionality since it is irrelevant
 }



Is there a way I can do this? Any help would be great 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
1
Best Answer

Answer by ArkaneX · Nov 26, 2013 at 02:57 PM

Coroutines can only be run in classes derived from MonoBehaviour. Please copy your function to such class, attach it to your menu (or any other object, where you have menu variable) and then test it.

EDIT: you can have your coroutine defined in static class, but you have to call it from standard class derived from MonoBehaviour. Calling it directly using

 Menu.FadeOut()

will not work in your case, but if you explicitly use StartCoroutine, this will work

 StartCoroutine(Menu.FadeOut())
Comment
Add comment · Show 5 · 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 thaiscorpion · Nov 26, 2013 at 03:03 PM 0
Share

When adding the script to an object I get an error:

Can't add script behaviour $$anonymous$$enu.

The script class can't be abstract.

avatar image ArkaneX · Nov 26, 2013 at 03:08 PM 0
Share

How is your class declared inside script?

avatar image thaiscorpion · Nov 26, 2013 at 03:11 PM 0
Share

I'm not really declaring it, just calling it like this:

menu.FadeOut();

I wanted it that way so I could access it easily from any script. This works for me with other classes that havent needed coroutines.

avatar image ArkaneX · Nov 26, 2013 at 03:34 PM 0
Share

Answer edited.

avatar image thaiscorpion · Nov 26, 2013 at 03:43 PM 0
Share

Oh great that is perfect just what I was looking for thanks!!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

17 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

Related Questions

Question about Coroutines in a Class Function 1 Answer

Finding the length of an enum in a different class 1 Answer

How do I delay the appearance of these buttons? 3 Answers

Unity call couroutine of child object by parent 0 Answers

script needs to derive from monobehaviour 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges