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 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.

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

Reading and Storing External Data into Memory (From Text) 1 Answer

Referencing subclass in Unity JS 2 Answers

Creation of RPG Class Stat Presets? 1 Answer

How to add javascript classes? 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