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 avidesk · Sep 14, 2012 at 12:34 AM · mousecoroutineclickeventschain

Coroutine Chain of Events

I have functionality in my game where an action has several steps in order to be completed.

I want it to perform this sequence:

  1. Click button X to start the chain of events. (Start coroutine #1)

  2. Click button to complete Step 1.

  3. Click button to complete Step 2.

  4. Click button to complete the process.

Here is a sketch I drew up to try to convey what I mean: alt text

'Do Something' is the normal operation of the object, and should be skipped completely when in the middle of this multi-step process.

I tried nesting co-routines but clearly did something wrong, what happens now is that all of the steps complete on the first click.

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
0

Answer by spiceboy9994 · Sep 14, 2012 at 02:36 PM

Hey Avidesk:

I used this http://answers.unity3d.com/questions/25550/chaining-actions-over-time-and-in-order.html to came up with a chained event manager script. Basically its a monobehavior that has a list of routine names.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class ChainedEventsManager : MonoBehaviour {
 
     public int EventId;    
     public List<string> ChainedEvents;
     public bool IsExecuting = false;
 //    
     public void NextTask(int nextId)
     {
        EventId = nextId;
        //audio.Play();
     }
 
     void Update()
     {
            if(EventId >= 0 && !IsExecuting)
         { 
             IsExecuting = true;
             //Execute Next chained action          
               SendMessageUpwards(ChainedEvents[EventId]);     
             //if is the last item on the chain, set the event id to -1 so the cycle is not hit anymore
             if (EventId == ChainedEvents.Count)
             {
                 EventId = -1;
             }
           
            }
     }
 }


Then I used the ChainedEvents list to set up the names of the functions that I want to execute on the correct order. So lets say the order if chained events list is ["FunctionA", "FunctionB"] I have something like this:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 
 public class ChainedEventsPuzzle: MonoBehaviour {
   private ChainedEventsManager eventManager;
   
   void Start()
   {
      eventManager = transform.GetComponent<ChainedEventsManager>();
   }
 }
 
   void StartChainedEvents()
   {
       //Set the id to 0, so the chain starts
       eventManager.NextTask(0);
   }
 
   void FunctionA()
   {
      //Do Function A
      eventManager.NextTask(1);
      eventManager.IsExecuting = false;
   }
 
    //Async function
   void FunctionB()
   {
      //Do function B
 
      //Call execution Async
      StartCoroutine(DoSomethingAsync();
   }
 
   IEnumerator DoSomethingAsync()
   {
      //Do Something
 
      yield return new WaitForSeconds(SecondsToWait); //I use this to wait an animation to end
      //Break the chained events    
     eventManager.NextTask(-1);
     eventManager.IsExecuting = false;
      
   }
 
 }

At anytime you could break the event chain by executing eventManager.Nextask(-1). I guess you could set your own flags for the buttons to know when the chainmanager should continue with the next chained even.

Hope it helps.

Comment
Add comment · 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

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

11 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

Related Questions

Player to look at 0 Answers

Fire missile to mouse click 1 Answer

Move object towards mouse but if mouse moves object fallows it's original path 0 Answers

How to detect double and single clicks using javascript? 1 Answer

Move at mouse click 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