Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by RazvanUzum · Aug 01, 2016 at 03:21 PM · c#gamecoroutinefunctionbug-perhaps

WaitForSeconds is not waiting for seconds, at all. Its like it doesnt exist.

using UnityEngine; using UnityEngine.UI; using System.Collections;

namespace Complete { public class ShootingScript : MonoBehaviour {

     public Rigidbody m_Shell;                   
     public Transform m_FireTransform;           
     public Slider m_AimSlider;                  
     public AudioSource m_ShootingAudio;         

     public AudioClip m_FireClip;                
     public float m_MinLaunchForce = 2000f;       

     public Text bulletsLeftText;
     public int LeftBullets = 30;
     public int TotalBullets = 120;



     private float m_CurrentLaunchForce;         

     private bool m_Fired;                      


     private void OnEnable()
     {
         
         m_CurrentLaunchForce = m_MinLaunchForce;
         m_AimSlider.value = m_MinLaunchForce;
     }


     private void Start ()
     {    
         
     }


     private void Update ()
     {    bulletsLeftText.text = " ";
         bulletsLeftText.text += LeftBullets;
         bulletsLeftText.text += "/";
         bulletsLeftText.text += TotalBullets;

         m_AimSlider.value = m_MinLaunchForce;


     }

     public void Reload()
     {    
         StartCoroutine (reloading ());
         LeftBullets = 30;
         TotalBullets = TotalBullets - 30;
     }
     public void ShootToKill()
     {
         
         while (0 < LeftBullets) 
         {
             Fire ();

             StartCoroutine (waitbetweenshots());

         }
         Reload ();
     }
     public void Fire ()
     {
         
             Rigidbody shellInstance =
                 Instantiate (m_Shell, m_FireTransform.position, m_FireTransform.rotation) as Rigidbody;


             shellInstance.velocity = m_CurrentLaunchForce * m_FireTransform.forward; 


             m_ShootingAudio.clip = m_FireClip;
             m_ShootingAudio.Play ();


             m_CurrentLaunchForce = m_MinLaunchForce;
             
                 LeftBullets= LeftBullets-1;

             

     }
     IEnumerator waitbetweenshots()
     {
         yield return new WaitForSeconds (0.25f);

     }
     IEnumerator reloading()
     {
         yield return new WaitForSeconds (2);
     }
 }}


It doesnt wait any time at all I want it to wait 0.25 secs between shots and 2 secs while reloading . and this doesnt work can you help me repair it or give me another idea on how to do this please ?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Landern · Aug 01, 2016 at 03:38 PM

Go and take a peek at the Coroutine examples, in particular look at the second example. When yielding internally to the method and not yielding outside when Starting the Coroutine will allow the continuation to happen within say your "waitbetweenshots" method but continue to the next line after the StartCoroutine. You will need to yield and set the calling method to IEnumerable, that will cause the continuation after X seconds to continue at that location instead of continuing as it does now. Again look at the second example and a simple refactor is all you need.

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# | How to delay a method with parameters 2 Answers

How to pause a Coroutine? 5 Answers

how do i Disable and Enable buttons? 2 Answers


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