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 /
avatar image
1
Question by TicTac · Apr 28, 2013 at 06:19 PM · yieldwaitforseconds

Yield WaitForSeconds never finishes?

I'm having trouble with this section of code. The code words up to Debug.Log("Power Up") and then doesn't go any further and I don't get Debug.Log("POWER DOWN")

function OnCollisionExit(collision : Collision){

        if (collision.collider.tag == "player") {
                Destroy (gameObject);
             Player.speed = 40;
             Debug.Log("POWER UP");
             yield WaitForSeconds (5);
             Player.speed = 25;
             Debug.Log("POWER DOWN");
         }
   }
   
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
2
Best Answer

Answer by whydoidoit · Apr 28, 2013 at 06:20 PM

You've destroyed the GameObject at the beginning. Therefore the coroutine will never execute after it yields and the object is really obliterated.

You probably want to hide it rather than destroy it at the top, then Destroy after the yield.

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 whydoidoit · Apr 28, 2013 at 08:11 PM 0
Share
 function OnCollisionExit(collision : Collision){
 
        if (collision.collider.tag == "player") {
          if(collider) collider.enabled = false;
          if(renderer) renderer.enabled = false;
          Player.speed = 40;
          Debug.Log("POWER UP");
          yield WaitForSeconds (5);
          Player.speed = 25;
          Debug.Log("POWER DOWN");
          Destroy(gameObject);
        }
   }
avatar image TicTac · Apr 28, 2013 at 08:18 PM 0
Share

Perfect, thanks! For anyone who happens to pass by, I changed Destroy (gameObject); to renderer.enabled = false;

avatar image
0

Answer by sfc.itzhak · Apr 28, 2013 at 07:32 PM

Hey ,

when you call the destroy(gameobject) you kill the gameobject with the script the power up is being called cause it waits till the end of the frame to delete the game object

function OnCollisionExit(collision : Collision){

    if (collision.collider.tag == "player") {
       
      Player.speed = 40;
      Debug.Log("POWER UP");
      yield WaitForSeconds (5);
      Player.speed = 25;
      Debug.Log("POWER DOWN");

Destroy (gameObject); } }

this will work

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 whydoidoit · Apr 28, 2013 at 08:09 PM 0
Share

But probably won't have the desired effect of hiding the pickup - which will continue to be able to be collided with.

avatar image sfc.itzhak · Apr 28, 2013 at 08:16 PM 0
Share

lol how do you know its a pickup and what he wants?

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

14 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

Related Questions

Why Is yield return new WaitForSeconds() not working 2 Answers

Definition of Script.function() depends on Script.function()... 3 Answers

About removing the backlight of a button after a set of time 2 Answers

Yield waitforseconds not consistent for animation-unity2D 0 Answers

Script gets stuck on WaitForSeconds() 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