- Home /
I keep getting:Trying to Invoke method: EnemySpawner.IncreaseSpawnrate couldn't be called.,I keep getting: Trying to Invoke method: EnemySpawner.IncreaseSpawnrate couldn't be called.
This is my code, what am i doing wrong?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class EnemySpawner : MonoBehaviour { public GameObject enemy;
float maxSpawnRateInSeconds = 5f;
// Start is called before the first frame update
void Start()
{
Invoke("SpawnEnemy", maxSpawnRateInSeconds);
InvokeRepeating("IncreaseSpawnrate", 0f, 30f);
}
// Update is called once per frame
void Update()
{
}
void SpawnEnemy()
{
Vector2 min = Camera.main.ViewportToWorldPoint (new Vector2 (0, 0));
Vector2 max = Camera.main.ViewportToWorldPoint (new Vector2 (1, 1));
GameObject anEnemy = (GameObject)Instantiate (enemy);
anEnemy.transform.position = new Vector2 (Random.Range (min.x, max.x), max.y);
ScheduleNextEnemySpawn ();
}
void ScheduleNextEnemySpawn()
{
float spawnInNSeconds;
if(maxSpawnRateInSeconds > 1f)
{
spawnInNSeconds = Random.Range(1f, maxSpawnRateInSeconds);
}
else
spawnInNSeconds = 1f;
Invoke("SpawnEnemy", spawnInNSeconds);
}
void IncreaseSpawnRate()
{
if (maxSpawnRateInSeconds > 1f)
maxSpawnRateInSeconds--;
if (maxSpawnRateInSeconds == 1f)
CancelInvoke("IncreaseSpawnRate");
}
}
Comment
Your answer