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 Diori · May 09, 2014 at 06:31 AM · coroutinedelayinstance

Cannot call Coroutine on another Gameobject.

I tried to call a coroutine from an instance of my enemy. But when I log the called IEnumerator function from the Enemy object, it return "this.gameObject" as my Player object.

 // This is the function called from my Player instance of "Character" class    
 void Launch(GameObject wpn)
 {    
 // I Called the enemy coroutine    
 BattleManager.instance.enemy.GetComponent<Character>().StartCoroutine(CharacterHitted(targetId,delayTime));    
 }
 
 
 // And this is the IEnumerator from same class.
 
 public IEnumerator CharacterHitted(int partsIdx, float delayTime)
     {
         yield return new WaitForSeconds(delayTime);
         Debug.Log("THIS : "+this.gameObject);    // returning my player GameObject 
     }


I want the "this" returning the Enemy instance. Maybe you have another trick to call function with arguments with delay?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by musaranya · May 09, 2014 at 06:52 AM

I think StartCoroutine(x) looks and triggers a function x in the same script. To call a coroutine from another script you can do something like this

http://answers.unity3d.com/questions/131210/c-how-to-make-a-script-talk-to-a-coroutine-in-anot.html

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 Diori · May 09, 2014 at 07:16 AM 0
Share

@musaranya Oh I see, that this behavior is normal for StartCoroutine. I thought we can call it from another object instances. It is a little bit messy to have an empty function that only call a Coroutine. Thanks for the answer

avatar image
0

Answer by Maerig · May 09, 2014 at 06:59 AM

Something like this, maybe ?

 private delegate void Callback(GameObject gameObject);

 public void MethodWhichUsesTheGameObject(GameObject gameObject) {
     // Your code
 }

 void Launch(GameObject wpn)
 {      
     BattleManager.instance.enemy.GetComponent<Character>().StartCoroutine(CharacterHitted(targetId,delayTime, MethodWhichUsesTheGameObject));    
 }

 private IEnumerator CharacterHitted(int partsIdx, float delayTime, Callback callback)
 {
     yield return new WaitForSeconds(delayTime);
     callback(gameObject);
 }
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

22 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

how can I add the delay between different function calls in C#.. 3 Answers

I'm having trouble with a coroutine delay 2 Answers

C# simple delay execution without coroutine? 2 Answers

strange behaviour for coroutine and invoke 1 Answer

Coroutine confusion 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