# StartCoroutine inside TriggerOnEnter2D isn't working

I am new to this, but I need help with a CSharp script. I need to collide with the desired object, pass the game to be 1.6 in TimeScale. After passing 8 seconds in the game, change back to 1 TimeScale, and try to call a Coroutine does not work. I really appreciate your help!

The code:

void TimeSet(){
Time.timeScale = 1.6f;
}

IEnumerator Normal (){
yield return new WaitForSeconds(8f);
Time.timeScale = 1f;
}

void OnTriggerEnter2D(){
TimeSet ();
StartCoroutine ("Normal");
}

• Are you sure that "OnTriggerEnter2d" is being called? Unity is a bit finicky about which meshes are allowed to trigger what. (See: docs.unity3d.com/Manual/CollidersOverview.html ) – Raven Dreamer Feb 15 '15 at 22:11
• Remember to include details about what's not working. "does not work" is not enough information. Also include what you've tried to do already to solve the problem. All of this information ensures you get the help you need. Edit the question to include this information. – MichaelHouse Feb 15 '15 at 22:11
• When Timescale is 1.6f, what would be 8 seconds? – SanSolo Jun 24 '15 at 9:20
• Debug step by step. First make sure OnTriggerEnter2D is actually called. Comment out everything in the method and just leave a Debug.Log("Triggered"); so you know it was called. – SanSolo Jun 24 '15 at 9:22

OnTriggerEnter2D is supposed to have a Collider2D parameter. As yours have none the method signature is different and that's probably why it's never called when a collision occurs.

Also as explained in the documentation I would strongly suggest to use StartCoroutine in it's method parameter form instead of using it with a string parameter.

I hope it helps.

EDIT

You should try by changing your OnTriggerEnter2D by:

void OnTriggerEnter2D(Collider2D othercollider)
{
TimeSet ();
StartCoroutine (Normal());
}


Also be sure that one of your game objects has a RigidBody2D

• Yes, the Collider2D is in the object and in the player, all set in "IsTrigger", to be safe , use a debug on to indicate when the player colliding with the object, if it detects a collision and changes the first value , but no coroutine starts. – Agus Feb 24 '15 at 2:10
• Sure. As my answer doesn't seem to be clear enough I'll edit it. – lvictorino Feb 24 '15 at 6:07
• When compiling the " OnTriggerEnter2D " function returns " The method must have a return type " This does not work :( Thanks 4 your help! – Agus Feb 25 '15 at 21:18
• OnTrigger do not need a method parameter to work. I believe your problem is elsewhere. im pretty sure I've seen other questions about calling coroutines from OnTriggerEnter methods but I can't remember if there was a solution. If you place a debug method in the trigger method, do you see it in the console? – Savlon May 25 '15 at 12:11
• The function not having parameter is irrelevant. The parameter is completely optional. You need the Collider2D reference if you are going to do something with it. Like destroying the collided object. Not including the parameter won't stop the function from being called. – SanSolo Jun 24 '15 at 9:14