- Home /
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!
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!
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?
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!
LOL! That's exactly it! How could I not seen this.. LOL. Thank you so much for your help. Really appreciate it!
did switching the order work? or did it just throw an error? just wondering.
Yeah it worked. But in the end I deleted the Destroy line. I do still need some function from the script.
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. }
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
Follow this Question
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