How do I make my GameObject stop spawning?

I have a space shooter game and I want to make my enemy (asteroids) stop spawning for a few seconds after my player dies and respawns. I tried this code:

public class DifficultyGUI : MonoBehaviour {
	public static float diffChosen = .5f;

void OnGUI()
	{
	if (GUI.Button(new Rect(400, Screen.height / 2, 100, 50), "Easy"))
	{
			diffChosen = 2f;
			SpawnScript.difficulty = 2f;
			Application.LoadLevel("game");
	}
	else if(GUI.Button(new Rect(600, Screen.height / 2, 100, 50), "Hard"))
	{
			diffChosen = .4f;
			SpawnScript.difficulty = .4f;
			Application.LoadLevel("game");
	}
}
}
public class SpawnScript : MonoBehaviour {
float timer = 0.0f;
bool isSpawning = false;
public static float difficulty = .5f;
	// Use this for initialization
	void Start () {

	}
	
	// Update is called once per frame
	void Update () {
    if (!isSpawning)
    {
        timer += Time.deltaTime;
    }
			//says how long it takes before an asteroid is spawned
    if (timer >= difficulty)
    {
        Spawn();
    }
    
	}
void Spawn()
{
		//makes program know that asteroids are spawning
    isSpawning = true;
		//says that an asteroid is spawned until there are 4 of them
    for (int i = 0; i < 3; i++)
    {
        Vector3 Pos = new Vector3(Random.Range(-0.81f, 0.81f), .4f, Random.Range(1f, 2f));
        Rigidbody newAsteroid = (Rigidbody)Instantiate(asteroidPrefab, Pos, Quaternion.identity);
        isSpawning = false;
        timer = 0;
    }
}

public class MoveScript : MonoBehaviour {
bool invuln = false;
   void OnCollisionEnter (Collision deadShip)
    {
    if (deadShip.gameObject.tag == "Asteroid") {
    	                invuln = true;
                        Respawn(); }
}

void Update () {
    if(invuln == true)
    		{
    			SpawnScript.difficulty = 0;
    			Debug.Log("difficulty = 0");
    			StartCoroutine(NoDeath());
    			Debug.Log("Coroutine began");
    		}
}

    IEnumerator NoDeath() {
    			yield return new WaitForSeconds(2);
    			float oldDiff = DifficultyGUI.diffChosen;
    			float realDiff = SpawnScript.difficulty;
    			realDiff = oldDiff;
    			Debug.Log("difficulty changed back to" + realDiff);
    			invuln = false;    
    		}
}

It doesn’t work for some reason; it just spawns a huge amount of asteroids as soon as my player touches the asteroid. What am I doing wrong and how do I fix it? P.S. if any more code is needed to answer the question, feel free to ask. P.P.S Sorry if I did something stupid: I’m only 14 and have only used Unity for a little over a week… I’m still learning.

When you die, you are setting ‘difficulty’ to 0. That combined with the way you are using ‘isSpawning’ (it doesn’t really do anything at the moment) is causing 3 enemies to be created during every Update call after you die.

You could use the same tricks you are using now to solve this. Set a bool ‘spawningAllowed’ to false when you get hit. Put everything in your Update inside an if so spawning only happens when ‘spawningAllowed’ == true. In the else-branch of that if (when spawning is not allowed) increment another timer and set ‘spawningAllowed’ back to true when enough time has passed.