How do I use a coroutine to spawn a gameobject every second. (error cs1502 & cs1503)?

1k Views Asked by At

I'm trying to force Unity to spawn a gameobject every second with a coroutine but I am getting the errors cs1502 & cs1503. (sorry if this is a stupid syntax error)

 public class BossCannon : MonoBehaviour 

 {


public BossBullet BossAmmo;
public float Force;
public bool trigger = false;




void Start () 
{

}



void Update()
{
    if (trigger == true)
    ShootBullet();
}

void ShotPattern()
{
    while (true)
    {  
        StartCoroutine(Shoot);
    }
}


public IEnumerator Shoot() 
{
    trigger = false;
    yield return new WaitForSeconds(1f); //Waits 1 second
    GameObject b = Instantiate(BossAmmo.gameObject, transform.position, transform.rotation) as GameObject;
    trigger = true; 
}

}

1

There are 1 best solutions below

0
On

Try passing the Transform to Instantiate instead of the GameObject.

Transform b = Instantiate(BossAmmo.transform, transform.position, transform.rotation) as Transform;

You are also starting your coroutine wrong. Try this:

StartCoroutine(Shoot());

The errors you are getting are Invalid Parameter errors. A method is expecting one type, and you are passing another type.

It might be helpful to post the full errors you are getting when you compile.

One more note: while(true) is generally a terrible thing to do. When you do get this running, I suspect Unity will suddenly lock up on you.