I’ve attempted to create an enemy spawner for this 2D arcade-style platformer defense game, but the spawner doesn’t produce any actual enemies, only blank game objects at position (0, 0, 0) that cannot move or be seen.
Here’s the code I’m using for the spawner. It’s very rudimentary, and could definitely be improved.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemySpawning : MonoBehaviour
{
[SerializeField] private GameObject enemy;
[SerializeField] private float startTime = 2.0f;
[SerializeField] private float spawnTime = 0.3f;
// Start is called before the first frame update
void Start()
{
enemy = new GameObject();
Vector2 spawnerPosition = new Vector2(transform.position.x, transform.position.y);
InvokeRepeating("spawnEnemy", startTime, spawnTime);
}
// Update is called once per frame
void spawnEnemy()
{
Instantiate(enemy, this.transform);
}
}
What should I change so that it spawns enemy GameObjects at the spawner location? Is this code too simple? I’d like to implement random y-axis spawn locations too, but I don’t know how to do that yet.