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
0
Question by roncel106 · Aug 05, 2012 at 09:59 AM · androidguiontriggerenteryield

Why is yield not working for me?

Here's my code:

function OnTriggerEnter(collider:Collider){

 var gameController = gameObject.Find("GameController").GetComponent(GameController);
     
 if(collider.gameObject.tag.Equals("Enemy"))
 {
     gameController.lives--;
 }

 if(gameController.lives == 0)
 {
     Instantiate(playerExplosion, transform.position, Quaternion.identity);
     Destroy(this.gameObject);

     yield WaitForSeconds(2);
       gameController.gameOver = true;
 
     
 }

}

heres how it goes, im trying to make a game over button. when gameController.gameOver = true, a gui button will appear in the screen, but the problem is, without the yield, it goes off instantly and it doesnt give the explosions to show themselves, so what i did was put a yield. when i put the yield on the code, the button doesn't show up in the screen. what seems to be the problem? could someone please explain it to me? :D

Heres the code for the game over button:

if(gameOver == true)
    {
        pauseGame();
        showPauseButton = false;
        if (GUI.Button (Rect((Screen.width *.50) - 65, (Screen.height * .50) - 22.5, 130,45), "Try Again")) {
            Application.LoadLevel(Application.loadedLevel);
            gameOver = false;
            showPauseButton = true;
            unpauseGame();
        }
    }

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
Best Answer

Answer by Eric5h5 · Aug 05, 2012 at 05:07 PM

You've destroyed the object, so it can't yield for 2 seconds since it doesn't exist anymore. Either don't destroy the object right away or call a coroutine on an object that doesn't get destroyed. You could teleport the object far away so it's out of the way:

 Destroy (gameObject, 2);
 transform.Translate (Vector3.right * 10000);
 yield WaitForSeconds(2);
 gameController.gameOver = true;

It's probably best just to make a coroutine on the GameController and call that, though:

 Destroy (gameObject);
 gameController.GameOverRoutine();

Where GameOverRoutine is this:

 function GameOverRoutine () {
     yield WaitForSeconds(2);
     gameOver = true;
 }
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 roncel106 · Aug 06, 2012 at 10:50 AM 0
Share

i love you :D

avatar image Eric5h5 · Aug 06, 2012 at 06:31 PM 0
Share

In a totally platonic way, I assume. ;) If this answer helped you, then you can click on the checkmark to accept it.

avatar image
-1

Answer by Dragonlance · Aug 05, 2012 at 10:15 AM

Hi Roncel,

you can not use yield WaitForSeconds in a Callback function. It only works in a Coroutine

WaitForSeconds can only be used with a yield statement in coroutines.

http://docs.unity3d.com/Documentation/ScriptReference/WaitForSeconds.html

So you should start a coroutine there and make the Trigger unfunctional with if(!gameOver){}

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 Eric5h5 · Aug 05, 2012 at 04:49 PM 1
Share

"OnTriggerEnter can be a co-routine, simply use the yield statement in the function." -- The Docs

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

10 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

Related Questions

How to make this function disable after 5seconds 1 Answer

[Android] Why does my enemy die no matter where I am in the 3D environment? 1 Answer

Level Button And Quit Button Android 2 Answers

GUI Texture Resolution 0 Answers

Scrolling Text 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