Increasing the speed for a few seconds doesn't work.

Hey guys.

If my player enter the trigger the speed should increase to 15 and after 10 seconds to 10 but with my script the speed goes to 10 instead of 15.

Here is the script:

using UnityEngine;
using System.Collections;

public class SpeedPickup : MonoBehaviour {

	public string tag = "Player";
	public PlayerControllerScript controller;

	void OnTriggerEnter2D(Collider2D other)
	{
		if (other.gameObject.tag == tag)
		{
			controller.maxSpeed = 15f;
			Destroy(gameObject);
			speedTime();
			revertSpeed();
		}
	}

	IEnumerator speedTime ()
	{
		yield return new WaitForSeconds(10);
	}

	void revertSpeed ()
	{
		controller.maxSpeed = 10f;
	}
}

If you destroy your gameObject before the end of the coroutine, the coroutine will stop. Put Destroy(gameObject) after the coroutine.

void OnTriggerEnter2D(Collider2D other)
{
  if (other.gameObject.tag == tag)
  {
     controller.maxSpeed = 15f;
     StartCoroutine(speedTime());
  }
}
 
IEnumerator speedTime ()
{
    yield return new WaitForSeconds(10);
    revertSpeed();
    Destroy(gameObject);
}
 
void revertSpeed ()
{
    controller.maxSpeed = 10f;
}