- Home /
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));
}
}
}
Any reason you are using FixedUpdate? You should move the hud = GameObject... out of Spawn if it needs to be called that ofen, put it in Start or make HudScript hud; public and drag/drop. The mix of calls to Spawn() within FixedUpdate and Invoke (which will loop multiple calls as well while playerScore = 5) looks problematic.
I used fixed update so that the playerScore would be updated every frame..If I use start it will just be taken at the first frame and never again no? I need it to check the score constantly so that when it reaches a certain value I invoke spawn
Please see this video on why you should not be using FixedUpdate
for this.
I don't see a reason why you would use Invoke
in your spawn function to call itself...
When you say "it isn't working", I just find that a very descriptive question. Has anyone ever called you and said: "$$anonymous$$y computer isn't working, fix it for me?". Isn't that a load of useless information. What isn't working?
Benproductions1, look at his $$anonymous$$arma, seems not to be the first Question he asked with a lot of useless information.
Answer by felixpk · May 11, 2014 at 11:43 AM
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!!
Your answer
Follow this Question
Related Questions
How to Spawn one object at a time in Transform Array 1 Answer
Unity Engine issue... 1 Answer
Way Point Assignment 4 Answers
How Do I Add An Instantiated Object To An Array? 3 Answers
Spawning Objects Using An Array. 1 Answer