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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Can't get past WaitForSeconds in my coroutine 1 Answer

Coroutine doesn't work when called from another method. 3 Answers

Need Help with a Null Reference 1 Answer

Delay after input? 1 Answer

How to make loop with call to IEnumerator actually pause? 2 Answers

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