1
\$\begingroup\$

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;
    Debug.Log ("Ready");
}

void OnTriggerEnter2D(){
    TimeSet ();
    StartCoroutine ("Normal");
}
\$\endgroup\$
4
  • \$\begingroup\$ 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 ) \$\endgroup\$ Feb 15, 2015 at 22:11
  • 4
    \$\begingroup\$ 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. \$\endgroup\$
    – House
    Feb 15, 2015 at 22:11
  • \$\begingroup\$ When Timescale is 1.6f, what would be 8 seconds? \$\endgroup\$
    – SanSolo
    Jun 24, 2015 at 9:20
  • \$\begingroup\$ 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. \$\endgroup\$
    – SanSolo
    Jun 24, 2015 at 9:22

1 Answer 1

1
\$\begingroup\$

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

\$\endgroup\$
7
  • \$\begingroup\$ 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. \$\endgroup\$
    – Agus
    Feb 24, 2015 at 2:10
  • \$\begingroup\$ Sure. As my answer doesn't seem to be clear enough I'll edit it. \$\endgroup\$
    – lvictorino
    Feb 24, 2015 at 6:07
  • \$\begingroup\$ When compiling the " OnTriggerEnter2D " function returns " The method must have a return type " This does not work :( Thanks 4 your help! \$\endgroup\$
    – Agus
    Feb 25, 2015 at 21:18
  • 1
    \$\begingroup\$ 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? \$\endgroup\$
    – Savlon
    May 25, 2015 at 12:11
  • 2
    \$\begingroup\$ 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. \$\endgroup\$
    – SanSolo
    Jun 24, 2015 at 9:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .