- Home /
Unity Coroutine problem
Good morning fellows members of the Unity community: I am having a problem with CORoutines in my C# script. In my script, I wrote down this script as my coroutine method:
 IEnumerator ShootMethod(float bulletspersecond) 
 {     
 yield return new WaitForSeconds(bulletspersecond);     
 //RayCast
 print("Shot");
 isShooting = false;     
 StopCoroutine("ShootMethod"); 
 }
And this script I use as the Coroutine starter:
     if(ShootingScript.belt[0].GetSetGameObject.active == true && isShooting == false && Input.GetMouseButton(0))
     {
         StartCoroutine(ShootMethod(2f));
         isShooting = true;
     }
But when I execute the program it doesn't print on the console "Shot" as you can see there on the first script.
Here it is the full script (if you guys need):
 public class ShootingScript : MonoBehaviour 
 {
     bool isShooting;
     
     public AudioSource coltShot;
     public AudioSource ak47Shot;
     public AudioSource m16Shot;
     
     public AudioSource coltReload;
     public AudioSource ak47Reload;
     public AudioSource m16Reload;
     
     public GameObject coltModel;
     public GameObject ak47Model;
     public GameObject m16Model;
     
     static public Weapon[] belt = new Weapon[3] {new Weapon(), new Weapon(), new Weapon()};
     
     IEnumerator ShootMethod(float bulletspersecond)
     {
         yield return new WaitForSeconds(bulletspersecond);
         //RayCast
         isShooting = false;
         StopCoroutine("ShootMethod");
     }
     // Use this for initialization
     void Start () 
     {
         
         //Setting the Weapon Specs
         //Colt .45
         ShootingScript.belt[0].SetGetName = "M1911";
         ShootingScript.belt[0].SetShootingAudio = coltShot;
         ShootingScript.belt[0].SetReloadingAudio = coltReload;
         ShootingScript.belt[0].SetRetMaxAmmo = 7;
         ShootingScript.belt[0].GetSetGameObject = coltModel;
         ShootingScript.belt[0].GetSetSeconds = 2f;
         ShootingScript.belt[0].GetSetGameObject.active = false;
         //AK47
         ShootingScript.belt[1].SetGetName = "Avtomat Kalashnikova 1947";
         ShootingScript.belt[1].SetShootingAudio = ak47Shot;
         ShootingScript.belt[1].SetReloadingAudio = ak47Reload;
         ShootingScript.belt[1].SetRetMaxAmmo = 40;
         ShootingScript.belt[1].GetSetGameObject = ak47Model;
         ShootingScript.belt[1].GetSetSeconds = 0.1f;
         ShootingScript.belt[1].GetSetGameObject.active = false;
         //M16
         ShootingScript.belt[2].SetGetName = "M16 Rifle";
         ShootingScript.belt[2].SetShootingAudio = m16Shot;
         ShootingScript.belt[2].SetReloadingAudio = m16Reload;
         ShootingScript.belt[2].SetRetMaxAmmo = 60;
         ShootingScript.belt[2].GetSetGameObject = m16Model;
         ShootingScript.belt[2].GetSetSeconds = 0.5f;
         ShootingScript.belt[2].GetSetGameObject.active = false;
         
         ShootingScript.belt[0].SetGetHasIt = true;
         ShootingScript.belt[1].SetGetHasIt = true;
         ShootingScript.belt[2].SetGetHasIt = true;
         
         
     }
     
     // Update is called once per frame
     void Update () 
     {
         //Changing weapons
         if(Input.GetButton("Handgun") && ShootingScript.belt[0].SetGetHasIt == true && ShootingScript.belt[0].GetSetActivated == false)
         {
             ShootingScript.belt[0].GetSetActivated = true;
             ShootingScript.belt[1].GetSetActivated = false;
             ShootingScript.belt[2].GetSetActivated = false;
         }
         else if(Input.GetButton("Machinegun") && ShootingScript.belt[1].SetGetHasIt == true && ShootingScript.belt[1].GetSetActivated == false)
         {
             ShootingScript.belt[0].GetSetActivated = false;
             ShootingScript.belt[1].GetSetActivated = true;
             ShootingScript.belt[2].GetSetActivated = false;
         }
         else if(Input.GetButton("Submachinegun") && ShootingScript.belt[2].SetGetHasIt == true && ShootingScript.belt[2].GetSetActivated == false)
         {
             ShootingScript.belt[0].GetSetActivated = false;
             ShootingScript.belt[1].GetSetActivated = false;
             ShootingScript.belt[2].GetSetActivated = true;
         }
         
         if(ShootingScript.belt[0].GetSetActivated == true)
         {
             transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, 6.8f);
             ShootingScript.belt[0].GetSetGameObject.active = true;
             ShootingScript.belt[1].GetSetGameObject.active = false;
             ShootingScript.belt[2].GetSetGameObject.active = false;
             StopAllCoroutines();
         }
         else if (ShootingScript.belt[1].GetSetActivated == true)
         {
             transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, 10.3f);
             ShootingScript.belt[0].GetSetGameObject.active = false;
             ShootingScript.belt[1].GetSetGameObject.active = true;
             ShootingScript.belt[2].GetSetGameObject.active = false;
             StopAllCoroutines();
         }
         else if(ShootingScript.belt[2].GetSetActivated == true)
         {
             transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, 10.3f);
             ShootingScript.belt[0].GetSetGameObject.active = false;
             ShootingScript.belt[1].GetSetGameObject.active = false;
             ShootingScript.belt[2].GetSetGameObject.active = true;
             StopAllCoroutines();
         }
         
         //Shooting Scripting itself
         if(ShootingScript.belt[0].GetSetGameObject.active == true && isShooting == false && Input.GetMouseButton(0))
         {
             StartCoroutine(ShootMethod(2f));
             isShooting = true;
         }
     }
 }
Hope I made myself clear Greetings Another fellow member of the Unity Community.
Ps.: While running the script, Unity doesn't show any errors or warnings. PsPs.: Sorry for my bad english.
if you put a Debug.Log( "enter") before yield instruction, console print it ?
And btw you can't stop your coroutine with Stopcoroutine( string coroutineName ) if you start your coroutine with StartCoroutine( IEnumerator coroutineFunction )
There is no StopCoroutine function for coroutine start with an IEnumerator as parameter.
Answer by aldonaletto · Feb 16, 2013 at 04:33 PM
Apparently, your code should work - but the value you're passing as bulletspersecond is actually "seconds per bullet": it's being passed to the delay routine, thus the value 2 means 2 seconds between shots, no 2 shots per second. Additionally, you don't need to stop the coroutine - it simply ends at the last closing bracket (I'm not sure, but suspect that using StopCoroutine to stop itself may even cause an error). Remove that StopCoroutine and try again - by the way, @liszto is right: you can only stop a coroutine when it was started with StartCoroutine("name"), and all coroutines with the same name will be stopped.
You are right about the bulletspersecond In fact, while writing my question I thought about this... Well. I deleted the stop CoROutine and tried again but it didn't work. After thinking a while I found the error. When I click the $$anonymous$$ouse button 0 it will wait 2 seconds and then shoot, where it should wait two seconds after the $$anonymous$$ouse button was pressed and don't let the player shot again within those 2 seconds. So to correct the script I just changed:
 IEnumerator Shoot$$anonymous$$ethod(float bulletspersecond)
 {
     //RayCast
     yield return new WaitForSeconds(bulletspersecond);
     isShooting = false;
 }
BUT, after waiting for 2 seconds (if it waits anyway...) the script doesn't change the isShooting var to false, it just keeps that way (true). But it's better we start a discussion some place else because here we have "answers" here...
But thanks for the help Aldo Naletto and agoliszto. That's all I needed.
Your answer
 
 
             Follow this Question
Related Questions
Weird problem with gunscript 2 Answers
MonoDevelop doesn't open script and keeps restarting? 0 Answers
Bug with my enemyAI 2 Answers
Make player unable to shoot when reloading 3 Answers
I have a problem with fire and reload in my GUN SCRIPT 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                