Hey, I am pretty new to Unity and all, I’ve been playing around with it in school for last 2-3 weeks.
I am creating a game that spawns enemies and counts your score for every enemy killed. I am using a coroutine to spawn the enemies, but now I wanted to change the amount of enemies spawned per second, after you killed a few of them. Problem is if I try to use the line "
ScoreManager testScore = GameObject.Find(“Scores”).GetComponent(); "
it calls it infinite amount of times and it eats up my ram until I go into Task Manager and turn it off.
So the question I have is how can I call the ScoreManager line only once?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnManager : MonoBehaviour
{
[SerializeField]
private GameObject _enemyPrefab;
void Start()
{
StartCoroutine(SpawnRoutine());
}
private IEnumerator SpawnRoutine()
{
while (true)
{
float randomX = Random.Range(-21f, 21f);
Instantiate(_enemyPrefab, new Vector3(randomX, 15f, 0),
Quaternion.identity);
ScoreManager testScore = GameObject.Find("Scores").GetComponent<ScoreManager>();
if ((testScore.score > 0) && (testScore.score < 10))
{
yield return new WaitForSeconds(1.5f);
}
if ((testScore.score > 10) && (testScore.score < 20))
{
yield return new WaitForSeconds(1f);
}