Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by ¢Stephano¢ · Feb 16, 2013 at 03:34 PM · bugguncoroutines

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image liszto · Feb 16, 2013 at 04:18 PM 1
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ¢Stephano¢ · Feb 16, 2013 at 09:21 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges