• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

How to use IEnumerator? Pls help! 1 Answer

How to skip WaitForSeconds? 1 Answer

IENumerator does not work 0 Answers

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

How to add multiple yield return new wait for seconds inside an IEnumerator? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges