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 /
  • Help Room /
avatar image
0
Question by SirBlazinGames · Feb 24, 2016 at 05:07 PM · c#waitforsecondsnoobienumeratorwait

Trying to delay code, nothing works.

I know there are already a TON of these but nothing is working and I REALLY want to accomplish this so please help me friends.

So what I am trying to make is a platform that when the player touches it the platform will disappear after a few seconds, so what I do is I make one of the IEnumerator wait function thingies everyone says to make but when I call it nothing happens. The platform just disappears immediately.

Here is the code, how can I fix?:

 void OnTriggerEnter(Collider other) {
         if (other.gameObject.CompareTag("Fall"))
         {
             StartCoroutine(Wait());
             other.gameObject.SetActive (false);
         }
 }

     IEnumerator Wait()
     {
         while (true)
         {
             yield return new WaitForSeconds(30000000);
 
         }
     }
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
1

Answer by Edy · Feb 24, 2016 at 05:23 PM

Move the sentence you want to delay to the Wait method after the WaitForSeconds sentence. Pass the other collider as parameter. Also, there's no need for the "while (true)" loop:

 void OnTriggerEnter(Collider other) 
 {
     if (other.gameObject.CompareTag("Fall"))
     {
         StartCoroutine(Wait(other));
     }
 }

 IEnumerator Wait(Collider other)
 {
     yield return new WaitForSeconds(3);
     other.gameObject.SetActive (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
avatar image
1

Answer by the_genius · Feb 24, 2016 at 05:27 PM

A void cannot be delayed. The StartCoroutine(Wait()) line just starts the coroutine, it doesn't wait for it to finish. You need to move the SetActive(false) into the IEnumerator after the delay.

 GameObject triggerObj; //This just stores the trigger object so it can be accessed in the IEnumerator
 
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.CompareTag("Fall"))
         {
             triggerObj = other.gameObject;
             StartCoroutine(Wait());
         }
     }
     IEnumerator Wait()
     {
             yield return new WaitForSeconds(30000000);
             triggerObj.gameObject.SetActive(false);
     }
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 SirBlazinGames · Feb 25, 2016 at 03:06 AM 0
Share

That worked well, thank you! ^_^

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

IENumerator does not work 0 Answers

Trying to add a delay to an Instantiate in a For Loop 0 Answers

How to skip WaitForSeconds? 1 Answer

How to use IEnumerator? Pls help! 1 Answer

How to add multiple yield return new wait for seconds inside an IEnumerator? 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