• 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 Craftist · Oct 03, 2017 at 12:31 PM · waitforsecondscoroutinescoroutine errorsenumerate

The code after WaitForSeconds is not working

Hello everyone. I'm making a runner game, so I need a powerup that makes you jump higher for 3 seconds. I've made a sprite with a box collider trigger that starts the DoubleJump coroutine. There's the code of the script that's attached to the sprite:

     private void OnTriggerEnter(Collider other)
     {
         Destroy(GetComponent<Transform>().gameObject);
         if (other.CompareTag("Player")) {
             StartCoroutine(DoubleJump(other));
         }
     }
 
     IEnumerator DoubleJump(Collider other)
     {
         var obj = other.GetComponent<RigidbodyFirstPersonController>().movementSettings;
         obj.JumpForce *= 2;
         Debug.Log("before return");
         yield return new WaitForSeconds(1);
         Debug.Log("after return");
         obj.JumpForce /= 2;
     }

After colliding with the sprite it disappears, jump force is doubling, "before return" shows in a log but nothing really happens after that, so the code after "yield return new WaitForSeconds(1);" is not evaluating. Can you please tell me why and fix the problem? Thank you.

Comment
Add comment · Show 4
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 Positive7 · Oct 03, 2017 at 01:51 PM 0
Share

if you destroy the gameObject that the script is attached to then the script is destroyed too. in your OnTrigger disable the sprite and destroy the gameObject when coroutineis finished.

avatar image Craftist Positive7 · Oct 03, 2017 at 01:58 PM 0
Share

Thank you, what worked!

avatar image Craftist Positive7 · Oct 03, 2017 at 02:01 PM 0
Share

Wait a minute... if I disable a gameobject, I can't start a coroutine...

     private void OnTriggerEnter(Collider other)
     {
         GetComponent<Transform>().gameObject.SetActive(false);
         if (other.CompareTag("Player")) {
             StartCoroutine(DoubleJump(other));
         }
     }
 
     IEnumerator DoubleJump(Collider other)
     {
         var obj = other.GetComponent<RigidbodyFirstPersonController>().movementSettings;
         obj.JumpForce *= 2;
         Debug.Log("before return");
         yield return new WaitForSeconds(1);
         Debug.Log("after return");
         obj.JumpForce /= 2;
         Destroy(GetComponent<Transform>().gameObject);
     }

It shows an error: "Coroutine couldn't be started because the the game object '2x-jump-powerup' is inactive!"

avatar image Positive7 Craftist · Oct 03, 2017 at 02:05 PM 0
Share

Don't disable the gameObject only the sprite

 private void OnTriggerEnter(Collider other)
      {
         GetComponent<Image>().enabled = false;
          if (other.CompareTag("Player")) {
              StartCoroutine(DoubleJump(other));
          }
      }
  
      IEnumerator DoubleJump(Collider other)
      {
          var obj = other.GetComponent<RigidbodyFirstPersonController>().movementSettings;
          obj.JumpForce *= 2;
          Debug.Log("before return");
          yield return new WaitForSeconds(1);
          Debug.Log("after return");
          obj.JumpForce /= 2;
          Destroy(gameObject);
      }
 

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Hellium · Oct 03, 2017 at 02:04 PM

Make the RigidbodyFirstPersonController run the coroutine instead of the power up :

 if (other.CompareTag("Player")) {
      other.GetComponent<RigidbodyFirstPersonController>().StartCoroutine(DoubleJump(other));
  }
Comment
Add comment · Show 3 · 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 Craftist · Oct 03, 2017 at 02:11 PM 0
Share

Can't find the method for some reason.

avatar image Hellium Craftist · Oct 03, 2017 at 02:32 PM 0
Share

Oops, my bad. Only $$anonymous$$onoBehaviours can run coroutines. Try this ins$$anonymous$$d :

 other.GetComponent<RigidbodyFirstPersonController>().StartCoroutine(DoubleJump(other));
avatar image Positive7 · Oct 03, 2017 at 02:15 PM 0
Share

Good idea! I should've suggest that too.

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

117 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

Related Questions

Add points every five seconds 1 Answer

Do something for 10 sec 2 Answers

Coroutine not executing second time 0 Answers

Is it more efficient detect when audio clip ends with AudioSource.isPlaying or with AudioClip.length? 0 Answers

Why Unity docs do not use New with WaitForSeconds and yield? 1 Answer

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