You are destroying the GameObject on which this script is running as soon as it collides with an enemy. (Destroy (gameObject);
)
That means it can't wait 3 seconds before loading the next scene - in 3 seconds' time, it will no longer exist and so it can't run any scripts at all!
You'll want to either delay the object's destruction until after the delay, or move the delay logic into a script you don't destroy (like a game state manager of some kind)
Edit: here's an example...
We can put a ScoreManager somewhere else in our scene, on an object we don't plan to destroy:
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
public class ScoreManager : MonoBehaviour
{
// This lets other scripts in the scene find this single instance without searching.
static ScoreManager _instance;
public ScoreManager GetInstance() { return _instance; }
// Inspector variables so we can view our score, and configure scene loading.
public int score;
public string nextSceneName;
public float waitTime;
// Ensure the instance is ready by the time it's needed.
void Awake()
{
_instance = this;
}
// This replaces the work that the ball was doing previously.
public void ScorePointsAndEndScene(int pointsScored)
{
score += pointsScored;
StartCoroutine(WaitAndLoadScene());
}
IEnumerator WaitAndLoadScene()
{
yield return new WaitForSeconds(waitTime);
SceneManager.LoadScene(nextSceneName);
}
}
Now your ball script can just worry about destroying itself, leaving the score & scene management to your other script:
void OnCollisionEnter2D(Collision2D col)
{
Debug.Log ("Collision");
if (col.gameObject.CompareTag("Enemy"))
{
ScoreManager.GetInstance().ScorePointsAndEndScene(20);
Destroy(gameObject);
}
}