How to Spawn objects at a certain score.

I’m making an infinite runner and I’d like to be able to control what objects can be spawned when the player reaches a certain score. I’d like to start spawning the objects in the array (as shown in script) when the player score reaches 5. This is my script so far but it isin’t working(It doesnt give me any errors either)

using UnityEngine;
using System.Collections;

public class SpawnScript : MonoBehaviour {

	public GameObject[] obj;
	public float spawnMin = 1f;
	public float spawnMax = 2f;

	float Score;

	HUDScript hud;
	
	void FixedUpdate ()
	{
		Spawn();
	}

//
//		hud = GameObject.Find("Main Camera").GetComponent<HUDScript>();
//
//		if(hud.playerScore == 5)
//			gameObject.SetActive(false);

	
	void Spawn()
	{
		hud = GameObject.Find("Main Camera").GetComponent<HUDScript>();

		if(hud.playerScore == 5)
		{
			Instantiate(obj[Random.Range (0, obj.GetLength(0))], transform.position, Quaternion.identity);
			Invoke ("Spawn", Random.Range (spawnMin, spawnMax));
		}
	}
}

public GameObject obj;
private float spawnMin = 1f;
private float spawnMax = 2f;
private HUDScript hud;
private bool spawn;

	void Start() {
		hud = Camera.main.GetComponent<HUDScript>(); //you can get the reference directly from the camera

		StartCoroutine("SpawnRoutine"); //Starting a coroutine to be able to wait for seconds as you might not want to spawn every frame
	}

	IEnumerator SpawnRoutine() {
		while(spawn) {
			yield return new WaitForSeconds(5); //waits 5 secs to execute the code below
			Instantiate(obj[Random.Range(0, obj.Length-1)], transform.position, Quaternion.identity); // I think length-1 but not sure here
		}
	}

	void Update() {
		if(hud.playerScore >= 5) {
			spawn = true;
		}
	}

People really just can guess what your problem is but I tried to understand what you need and fixed your code.
And I really don’t know where these “objects” should spawn, or what spawnMin, spawnMax should be.

Make sure you:

  • use matching names for your variables
  • write precisely what your problem is
  • and explain precisely what your code should be doing!!