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 /
  • Help Room /
avatar image
0
Question by SakiHub · Jan 28 at 07:20 PM · waitforseconds

Problems WaitForSeconds

Hey i'm quite new to unity and everything, can someone please help me and tell me why after the WaitForSeconds spawner, enemy1 and enemy2 do not set to true and Debug.Log("Timer Finished") is not called?

Thanks!

  public GameObject effect;
  public GameObject powerUpEffect1;
  public GameObject explosionSound;
  public GameObject spawner;
  public GameObject Enemy1;
  public GameObject Enemy2;
  
  
  private void OnTriggerEnter2D(Collider2D other)
  {
      if (other.CompareTag("Player"))
      {
          
          StartCoroutine(powerUp(other));
      }
  }
    
  private IEnumerator powerUp(Collider2D player)
  {
      Debug.Log("Timer started");
      Instantiate(effect, transform.position, Quaternion.identity);
      Instantiate(explosionSound, transform.position, Quaternion.identity);
      spawner.SetActive(false);
      Enemy1.SetActive(false);
      Enemy2.SetActive(false);
     
      powerUpEffect1.SetActive(true);
      
      GetComponent<SpriteRenderer>().enabled = false;
      GetComponent<CircleCollider2D>().enabled = false;
      yield return new WaitForSeconds(10.0f);
     
      Debug.Log("Timer Finished");
      
      spawner.SetActive(true);
      Enemy1.SetActive(true);
      Enemy2.SetActive(true);
   
      Destroy(gameObject);
  }


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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by jaca6767 · Jan 28 at 09:00 PM

I believe that with the way that yield works, a function containing a WaitForSeconds command cannot be marked as private. Removing this field should allow it to work properly.

Comment
Add comment · Show 2 · 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 SakiHub · Jan 28 at 09:17 PM 0
Share

First of all thanks @jaca6767 for answering me, I tried to write the code as you advised me but nothing changed. I noticed that if I enter a value higher than 3 seconds I get the problem I'm having, while if I enter a value equal to or less than 3 everything works perfectly. I don't really understand where the problem is :(

public class PowerUp : MonoBehaviour {

 public GameObject effect;
 public GameObject powerUpEffect1;
 public GameObject explosionSound;
 public GameObject spawner;
 public GameObject Enemy1;
 public GameObject Enemy2;
 
 



 void OnTriggerEnter2D(Collider2D other)
 {
     if (other.CompareTag("Player"))
     {
         
         StartCoroutine(powerUp(other));

     }
 }

   

 IEnumerator powerUp(Collider2D player)
 {
     Debug.Log("Timer started");

     Instantiate(effect, transform.position, Quaternion.identity);
     Instantiate(explosionSound, transform.position, Quaternion.identity);
     spawner.SetActive(false);
     Enemy1.SetActive(false);
     Enemy2.SetActive(false);
     powerUpEffect1.SetActive(true);

     

     GetComponent<SpriteRenderer>().enabled = false;
     GetComponent<CircleCollider2D>().enabled = false;


     yield return new WaitForSeconds(10);
    

     Debug.Log("Timer Finished");
     
     spawner.SetActive(true);
     Enemy1.SetActive(true);
     Enemy2.SetActive(true);

  


     Destroy(gameObject);
 }
avatar image jaca6767 SakiHub · Jan 28 at 09:45 PM 0
Share

I mean its a bad idea, but you could just write the command 4 times with a value of 3,3,3, and 1. A bodge but if that works, it'd be a temporary fix.

avatar image
0

Answer by SakiHub · Jan 29 at 11:55 AM

@jaca6767 i tried but it still not workind :(

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

Answer by jaca6767 · Jan 29 at 11:14 PM

Ok, I think I've found a replacement. It may be best to split it into two functions. Have the first part run activating the power up before invoking another function to disable it after a certain duration. You can find the documentation for invoke here.

A sample piece of code like this would be:

    void OnTriggerEnter(whatever)
    {
    //Call first stuff here
    Invoke(done, 10);
    }
    void done()
    {
    //deactivation here.
    }

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 SakiHub · Jan 29 at 11:50 PM 0
Share

i really don't know where's the problem, it doesn't let me wait more then 3 seconds. i also tried to use more invoke but nothing has changed

like this:

 void picked1()
     {
         Invoke("picked2", 3f);
     }
 
     void picked2()
     {
         Invoke("picked3", 3f);
     }
 
     void picked3()
     {
         Invoke("picked", 1f);
     }

I really don't know. If u also don't know don't worry man, thanks for your help anyways.

avatar image
0

Answer by privatecontractor · Jan 30 at 07:39 PM

Hi @SakiHub,

really weird problem, maybe you could try some detour? So instead of:

yield return new WaitForSeconds(10.0f);

Let's use:

 float m_remaingDelayTime = 10.0f;
 while (m_remainingDelayTime > 0)
 {
 m_remainingDelayTime -= Time.deltaTime;
 yield return null;
 }



Also, did you check is your Coorutine not running few times? I understand that you are sure that you Starts it only one time? Do you reciving Single Debug.Log("Timer starts") calls...

If not: Just add bool which will block multiple repetitions....

bool m_coorutineIsAlreadyGoing = false;

then in line where you starting coorutine, just change for:

if (!m_coorutineIsAlreadyGoing) StartCoroutine(powerUp(other))

And in Your Coorutine:

  IEnumerator powerUp(Collider2D player)
 {
 m_coorutineIsAlreadyGoing = true;
 //rest of your code...
 m_coorutineIsAlreadyGoing = false;
 }

Hope that will help, let me know!

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

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

IENumerator does not work 0 Answers

Problem with coroutine / create a gap of 5 seconds [C#] 1 Answer

How to implement pausing of WaitForSeconds based coroutine? 1 Answer

Call Coroutine step after step in foreach loop 0 Answers

reload script assemblies is VERY Slow. 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