I’m creating a simple game where all the player has to do is avoid red squares. It’s a 2d game and I’m still very new to unity and c# so I’m not completely sure what I’m doing. I keep running into this problem on my enemy spawn script “The Object you want to instantiate is null.”
public float maxTime = 1;
private float timer = 0;
private GameObject badGuy;
public float height = 12;
public object GameObject;
private GameObject newbadGuy;
void Start()
{
GameObject newbadGuy = Instantiate(badGuy);
newbadGuy.transform.position = transform.position + new Vector3(0, Random.Range(-height, height), 12);
}
void Update()
{
if (newbadGuy != null && timer > maxTime)
{
GameObject newbadGuy = Instantiate(badGuy);
newbadGuy.transform.position = transform.position + new Vector3(0, Random.Range(-height, height), 12);
Destroy(newbadGuy, 5);
timer = 0;
}
timer += Time.deltaTime;
}
This problem is making the enemy unable to spawn or destroy new enemies.