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
3
Question by meazant · Oct 31, 2011 at 08:43 PM · coroutinetimeoncollisionenterwaitforsecondsienumerator

OnCollisionEnter as IEnumerator problem (WaitForSecond)

Hello there! It's me with my seemingly endless Unity problems. This time, I want to create a WaitForSecond function inside an OnCollisionEnter function. So naturally I changed it into IEnumerator. But the WaitForSecond still doesn't occur. What seems to be the problem here?

     void Start() {
     //StartCoroutine("OnCollisionEnter", ????);
 }
 IEnumerator OnCollisionEnter (Collision coll) {
         if(coll.gameObject.tag == "bullet") 
     {
         hitPoints -= bulletDamage;
     } 
 
         if (hitPoints <= 0 && enemy == true)
     { 
         Instantiate(explosionPrefab, transform.position, transform.rotation);
         Destroy(gameObject); // YES, the object destroyed.
         yield return new WaitForSeconds(1); // NO, it didn't wait.
         Time.timeScale = 0; // NO, this didn't happen also.
         info.text = "YOU WIN!"; //NO.
     }
 }

As you can see I've also tried to call the function with StartCoroutine but I can't figure out what parameter should I put into the bracket. Sounds noobish? I am one. Any kinds of help appreciated!

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

Answer by GoSuNeem · Oct 31, 2011 at 09:11 PM

I would suggest that instead of trying to change the whole function of OnCollisionEnter, just use a Invoke().

http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.Invoke.html

 void OnCollisionEnter (Collision coll) {
   //do something
   Invoke("MyWaitingFunction",10); //this would make it wait 10 seconds then run the function called "MyWaitingFunction"
 }
 
 void MyWaitingFunction()
 {
   //Stuff here happens after 10 seconds of the collision.
 }

Good Luck!

Comment
Add comment · Show 9 · 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 meazant · Oct 31, 2011 at 10:16 PM 0
Share

Thank you for your suggestion, but I can't seem to make the Invoke function work too. Here's the code :

         if (hitPoints <= 0 && enemy == true)
     { 
         Instantiate(explosionPrefab, transform.position, transform.rotation);
         Destroy(gameObject);
         Invoke("YouWin",1);
         
     }

and the function :

 void YouWin() {
     Time.timeScale = 0;
     info.text = "YOU WIN!";
 }

But the timescale doesn't stop, just like what happen when I used WaitForSeconds. Any idea what might cause this?

avatar image GoSuNeem · Oct 31, 2011 at 10:23 PM 1
Share

Yeah... sorry should of read your script more carefully ;P

the code----- Destroy(gameObject); is causing the issue here.

When you destroy the object it stops the code at that point. so invoke never gets called.

1 of the few things you can do is just simply hiding your object. but i don't think you can do gameObject.enable = false because i think that also turns off your script. you can just try to turn the alpha of your texture 0 which is pretty hacky :[

If you could, try flipping the invoke function and the destroy function. It might give an error but doesn't hurt to try ;]

Edit: @aldonaletto good answer on dat ;D Votes!

avatar image meazant · Oct 31, 2011 at 10:31 PM 0
Share

LOL! That's exactly it! How could I not seen this.. LOL. Thank you so much for your help. Really appreciate it!

avatar image GoSuNeem · Oct 31, 2011 at 10:33 PM 0
Share

did switching the order work? or did it just throw an error? just wondering.

avatar image meazant · Oct 31, 2011 at 10:39 PM 0
Share

Yeah it worked. But in the end I deleted the Destroy line. I do still need some function from the script.

Show more comments
avatar image
3

Answer by aldonaletto · Oct 31, 2011 at 10:28 PM

You're destroying the gameobject to which this script is attached, thus nothing more will happen. If you really want to suicide this object, the instructions after Destroy should be in another script, attached to another object.

       ...
       Destroy(gameObject); // <- this object (and this script) get destroyed
       // this yield may start executing, but before the next update
       // the whole script will disappear
       yield return new WaitForSeconds(1);
       Time.timeScale = 0; // NO, this didn't happen also.
       info.text = "YOU WIN!"; //NO.
    }
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 meazant · Oct 31, 2011 at 10:33 PM 0
Share

Yeah, you're right! I'm such a noob for letting something like this happen. Thank you for you answer. Appreciate your time :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

WaitForSeconds Not Working 4 Answers

Problem with coroutine 2 Answers

IEnumerator manipulates variable in android build 1 Answer

(Rapid fire) How to accurately wait for a very short amount of time? 4 Answers

Returning an IEnumerator as an int? 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