- Home /
Trying to get my spawn manager to respond to the current score
So my spawn manager is working well with anything that does not need to respond to the current score.
My problem is that I want certain gameObjects to spawn based on what my current score is but it's not going well. I do have all my gameObjects properly attached to scripts, canvas etc.
I'm trying to call the score from the UIManager into my Spawn Manager to start the coroutine when the score is greater than or equal to 50.
UI Manager code: contains the score and updating of score.
public Text scoreText; public int score;
public void UpdateScore() { score += 10; scoreText.text = "Score:" + score; }
Spawn Manager Code: is calling to the uiManager to start the coroutine ArcherSpawnManager, the first 2 coroutines are working fine as the simply begin regardless of anything.
private UIManager _uIManager;
void Start()
{
_uIManager = GameObject.Find("Canvas").GetComponent<UIManager>();
StartCoroutine(MerchantSpawnRoutine());
StartCoroutine(PowerUpsSpawnRoutine());
if (_uIManager.score >= 50)
{
StartCoroutine(ArcherSpawnRoutine());
}
}
public IEnumerator ArcherSpawnRoutine() {
while (true)
{
yield return new WaitForSeconds(Random.Range(2, 4));
Instantiate(_archerBottomRight, new Vector3(8.2f, Random.Range(-4.25f, -.5f), 0), Quaternion.identity);
yield return new WaitForSeconds(Random.Range(2, 4));
Instantiate(_archerBottomLeft, new Vector3(-8.2f, Random.Range(-4.25f, -.5f), 0), Quaternion.identity);
yield return new WaitForSeconds(Random.Range(2, 4));
Instantiate(_archerTopRight, new Vector3(8.05f, Random.Range(.5f, 4.25f), 0), Quaternion.identity);
yield return new WaitForSeconds(Random.Range(2, 4));
Instantiate(_archerTopLeft, new Vector3(-8.11f, Random.Range(.5f, 4.25f), 0), Quaternion.identity);
}
}
Answer by AaronXRDev · Nov 02, 2018 at 08:49 PM
I would put the score check inside the ArcherSpawnRoutine instead of on Start. I would expect that at the start the player's score would always be 0.
while (true)
{
yield return new WaitForSeconds(Random.Range(2, 4));
if (_uIManager.score >= 50) { Instantiate(_archerBottomRight, new Vector3(8.2f, Random.Range(-4.25f, -.5f), 0), Quaternion.identity); }
yield return new WaitForSeconds(Random.Range(2, 4));
if (_uIManager.score >= 50) { Instantiate(_archerBottomLeft, new Vector3(-8.2f, Random.Range(-4.25f, -.5f), 0), Quaternion.identity); }
yield return new WaitForSeconds(Random.Range(2, 4));
if (_uIManager.score >= 50) { Instantiate(_archerTopRight, new Vector3(8.05f, Random.Range(.5f, 4.25f), 0), Quaternion.identity); }
yield return new WaitForSeconds(Random.Range(2, 4));
if (_uIManager.score >= 50) { Instantiate(_archerTopLeft, new Vector3(-8.11f, Random.Range(.5f, 4.25f), 0), Quaternion.identity); }
}
NO WAY! It finally worked. Thank you so much, this has been driving me up a wall for hours!