I have a problem in unity C# with my code, I have an error in instantiate inside a coroutine! Here’s my code:
using System.Collections;
using UnityEngine;
public class Enemies : MonoBehaviour
{
public float betweenSpawns;
public bool stop;
public GameObject Prefab;
public Transform enemy;
public PlayerMovement movement;
public void OnCollisionEnter(Collision col)
{
//Debug.Log(gameObject.name + " Has collided with " + col.gameObject.name);
Time.timeScale = 0.5f;
stop = true;
if (Input.GetKey("r"))
{
Time.timeScale = 1;
stop = false;
}
}
// Start is called before the first frame update
void Start()
{
StartCoroutine(Spawner(enemy));
}
// Update is called once per frame
IEnumerator Spawner(Transform enemy)
{
while (!stop)
{
Vector3 spawnPosition1 = new Vector3(Random.Range(-37.5f, -18.75f), 25, 0);
Vector3 spawnPosition2 = new Vector3(Random.Range(-18.75f, 0), 25, 0);
Vector3 spawnPosition3 = new Vector3(Random.Range(0, 18.75f), 25, 0);
Vector3 spawnPosition4 = new Vector3(Random.Range(18.75f, 37.5f), 25, 0);
//here problem with instantiate!
Instantiate(Prefab, spawnPosition1 + transform.TransformPoint(0, 0, 0), 0, 0, 0);
Instantiate(Prefab, spawnPosition2 + transform.TransformPoint(0, 0, 0), 0, 0, 0);
Instantiate(Prefab, spawnPosition3 + transform.TransformPoint(0, 0, 0), 0, 0, 0);
Instantiate(Prefab, spawnPosition4 + transform.TransformPoint(0, 0, 0), 0, 0, 0);
yield return new WaitForSeconds(betweenSpawns);
}
}
}