• 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 /
This question was closed Oct 09, 2014 at 08:06 AM by fafase for the following reason:

The question is answered, right answer was accepted

avatar image
2
Question by Mekanofreak · Oct 07, 2014 at 05:12 AM · buttoncoroutine4.6

Is it possible to start a coroutine using a button from the new UI system (4.6)

I tried Starting a simple coroutine using a button created with the new UI system, but everytime I have a yield in my fuction it stop appearing in the button's function selector drop down menu in the inspector, the coroutine is a really simple :

while(true) { do something; yield }

Is there something special i need to do or is it just impossible to start a coroutine with a button using the new UI system ?

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

  • Sort: 
avatar image
8
Best Answer

Answer by fafase · Oct 07, 2014 at 05:14 AM

You cannot directly called the coroutine since your method needs to return void but you can just use a wrapper.

 public void Wrapper(){
     StartCoroutine(MyCoroutine());
 }
 private IEnumerator MyCoroutine(){
     yield return null;
 }
Comment
Add comment · Show 4 · 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 Mekanofreak · Oct 08, 2014 at 10:23 PM 1
Share

Simple workaround, works like a charm, thanks !

avatar image fafase · Oct 09, 2014 at 08:06 AM 0
Share

As a side note you may know but in case, if you wish to prevent multiple calls.

 private bool isRunning = false;
 public void Wrapper(){
     if(isRunning == false){
        StartCoroutine($$anonymous$$yCoroutine());
     }
 }
 private IEnumerator $$anonymous$$yCoroutine(){
     isRunning = true;
     // Do your stuff over multiple frames
     yield return null;
     isRunning = false;
 }

avatar image Chino_96 · Aug 21, 2017 at 02:39 PM 0
Share

I know that this is old but i would be glad if you can tell me how can i attach a this to a button

avatar image Shiner252 Chino_96 · Aug 22, 2017 at 07:13 PM 0
Share

After you've added a UI Button component to an object, you should go into the button's inspector and drag the gameObject into the button component. After the gameObject is referenced, you can select a method from a dropdown menu. $$anonymous$$ake sure your method is public.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

4.6 UI: How to invoke button click on Enter key 2 Answers

How can a button be highlighted but not trigger on click event when submit button is pressed? 0 Answers

IPointerClickHandler error 2 Answers

4.6 How to get name of button that was clicked? 7 Answers

Coroutine stops working after first run 1 Answer

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