Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 delfinatarga · Nov 14, 2017 at 10:41 PM · scripting problemcoroutine

How to start a Coroutine in another script?

Hello! I did some research about this but what i found was kinda confusing so I didn't actually understand it. Basically I have a coroutine that displays an image after 5 seconds, but I want that image to display 5 seconds after an enemy spawns. I understand that what I need to do is call StartCoroutine in my enemy spawning function, but I'm very new to programming and i couldn't do it, even though it's probably something really easy.

Here's the code that displays the image:

 public class enemyAttack : MonoBehaviour {
 
     public Image TargetImage; 
 
     void Start ()
     {
         TargetImage.enabled = false;
         StartCoroutine(playerIsAttacked (5, TargetImage));
 
     }
 
 
     public IEnumerator playerIsAttacked (float t, Image im)
     {
         yield return new WaitForSeconds (t);
         TargetImage.enabled = true;
     }
 }


and this is the code that spawns the enemies:

 public class TimedSpawn : MonoBehaviour {
 
     public GameObject spawnee;
     public bool stopSpawning;
     public float spawnTime;
     public float spawnDelay;
 
     void Start () {
         InvokeRepeating ("SpawnObject", spawnTime, spawnDelay);
 
     }
 
     public void SpawnObject (){
         Instantiate (spawnee, transform.position, transform.rotation);
 
         if (stopSpawning) {
             CancelInvoke ("SpawnObject");
         }
     }
 }

Comment
Add comment · Show 1
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 EDevJogos · Nov 14, 2017 at 11:20 PM 1
Share

@delfinatarga Hmm, i'm kind lost here, enemyAttack is your spawned enemy? if so your code should work just fine, when it's istantiated Start method will be called and after 5s the TargetImage will be enabled.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by shadowpuppet · Nov 14, 2017 at 11:42 PM

A number of ways to do this...some better than others. I'm a bit of a novice as well so don't get parts of your script so I'll start from scratch. I'd have this script on the image disabled, then when the enemy spawns, enable it the script , and have your coroutine start in the OnEnable function rather than the start function. maybe start a second coroutine as well to turn off image then disable script again for next spawn. So in this script when enabled will wait 5 seconds, then show text, and 5 seconds later turn it off and disable the script again

  public class enemyAttack : MonoBehaviour {
  
      public Image TargetImage; 
  public int TextTimer= 5;
 
      void OnEnable ()
      {
          StartCoroutine(DelayBeforeText ());
      }
 private IEnumerator DelayBeforeText(){
         yield return new WaitForSeconds(TextTimer);
          TargetImage.enabled = true;
  StartCoroutine(DelayBeforeTextEnds ());
      }
 private IEnumerator DelayBeforeTextEnds(){
         yield return new WaitForSeconds(TextTimer);
   TargetImage.enabled = false;
 this.enabled = false;
  }
 }
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

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

DisplayText info and wait for animation ends to destroyobject ( time extra box) 0 Answers

Coroutines randomly stop working 0 Answers

I have chains of connectable objects which reference eachother. Need them to pass a simple boolean to eachother! How? 1 Answer

Coroutine not starting properly with enemy patrol script 2 Answers

Rerun a script after it finishes, when it already has multiple Coroutines? 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