Help with corutines
I want to make my enemy shoot every 1 second .
using UnityEngine; using System.Collections;
public class enemyshoot : MonoBehaviour {
public Transform spawnbulletenemy;
public Rigidbody bullet;
public float brzinametka = 5f;
// Use this for initialization
void Start () {
StartCoroutine (MyCorutine());
}
// Update is called once per frame
void Update () {
MyCorutine ();
}
IEnumerator MyCorutine()
{
Rigidbody metakkojileti;
metakkojileti = Instantiate (bullet, spawnbulletenemy.position, spawnbulletenemy.rotation) as Rigidbody;
metakkojileti.AddForce (spawnbulletenemy.forward * brzinametka);
yield return new WaitForSeconds (1);
}
}
This is my script , problem is that it shoots only 1 bullet and then it dosent anymore .
Answer by vittu1994 · May 17, 2016 at 09:25 AM
You gotta loop the contents in your function. Right now it will only spawn a bullet, shoot it and then wait a second and then its done. You want to repeat this process and to do this you need to wrap up your content into a loop.
Is the enemy gonna shoot all the time? No stopping? use while(true):
IEnumerator MyCouroutine()
{
while(true)
{
//your code
}
}
This way it will loop the content in your couroutine forever, unless the script gets disabled/destroyed.
Your answer
Follow this Question
Related Questions
C# Simple delay at void Update? 2 Answers
How to make pause beetwen action 0 Answers
Wait for 7 Seconds not working 1 Answer
Delay Animator Action in C#? 0 Answers
Are Coroutines the best way to delay ? If not, what is ? C# 2 Answers