yield WaitForSeconds not working

Discussion in 'Scripting' started by xxPillsxx, Oct 10, 2012.

  1. xxPillsxx

    xxPillsxx

    Member

    Joined:
    Oct 10, 2012
    Messages:
    21
    Hi,I'm creating a FPS game and I'm having problem with yield WaitForSeconds.Here is the part of my script where I used the yield wait for seconds
    Code (csharp):
    1.  
    2. function Fire () {
    3.     audio.Play();
    4.     if(CanFire == true){
    5.     var Bullit = Instantiate(Bullet,Spawn.position,Spawn.rotation);
    6.         Bullit.rigidBody.AddForce(transform.foward * 1000);
    7.         CanFire = false;
    8.         yield WaitForSeconds(FireRate);
    9.         CanFire = true;
    10.         AmmoLeft -= 1;
    11.     }
    12. }
    13.  
    14. function Reload(){
    15.     BroadcastMessage("ReloadAnim");
    16.     CanFire = false;
    17.     yield WaitForSeconds(ReloadTime);
    18.     CanFire = true;
    19. }
    Code (csharp):
    1.  
    2. var Bullet : Transform;
    3. var Spawn : Transform;
    4. var BulletSpeed : float = 1000;
    5. var ReloadTime : float = 2;
    6. var AmmoInMag : float = 30;
    7. var IsFullAuto = true;
    8. static var AmmoLeft : float;
    9. private var CanFire = true;
    10. var FireRate = 0.8;
     
  2. All_American

    All_American

    Member

    Joined:
    Oct 14, 2011
    Messages:
    1,069
    I don't see the reload function being called anywhere.
     
    Last edited: Oct 10, 2012
  3. xxPillsxx

    xxPillsxx

    Member

    Joined:
    Oct 10, 2012
    Messages:
    21
    But..I'm using Javascript...D:
     
  4. All_American

    All_American

    Member

    Joined:
    Oct 14, 2011
    Messages:
    1,069
    You have to call the reload function somewhere 'reload();"

    I am not seeing that in your code.

    Like

    Code (csharp):
    1.  
    2. /
    3. if(rounds == 0)
    4. {
    5. reload();
    6. }
    7. /
    8.  
     
  5. xxPillsxx

    xxPillsxx

    Member

    Joined:
    Oct 10, 2012
    Messages:
    21
    Code (csharp):
    1.  
    2. function Update () {
    3.     if(IsFullAuto == false){
    4.     if(Input.GetButtonDown("Fire1")){
    5.         if(AmmoLeft > 0){
    6.         BroadcastMessage("FireAnim");
    7.         Fire();
    8.             }
    9.         }
    10.     }
    11.     else{
    12.     if(Input.GetButton("Fire1")){
    13.         if(AmmoLeft > 0){
    14.         BroadcastMessage("FireAnim");
    15.         Fire();
    16.             }
    17.         }
    18.     }
    19.    
    20.    
    21.     if(AmmoLeft == 0)
    22.     {
    23.         Reload();
    24.     }
    25.    
    26.     if(AmmoLeft < 0){
    27.         AmmoLeft = 0;
    28.     }
    29. }
    30.  
     
  6. All_American

    All_American

    Member

    Joined:
    Oct 14, 2011
    Messages:
    1,069
    Is line 20 in your reload function??? or fire function??
     
  7. xxPillsxx

    xxPillsxx

    Member

    Joined:
    Oct 10, 2012
    Messages:
    21
    Dafuq man...it's in Update function,here is the full code
    Code (csharp):
    1.  
    2. var Bullet : Transform;
    3. var Spawn : Transform;
    4. var BulletSpeed : float = 1000;
    5. var ReloadTime : float = 2;
    6. var AmmoInMag : float = 30;
    7. var IsFullAuto = true;
    8. static var AmmoLeft : float;
    9. private var CanFire = true;
    10. var FireRate = 0.8;
    11.  
    12. function Start () {
    13.  AmmoLeft = AmmoInMag;
    14. }
    15.  
    16. function Update () {
    17.     if(IsFullAuto == false){
    18.     if(Input.GetButtonDown("Fire1")){
    19.         if(AmmoLeft > 0){
    20.         BroadcastMessage("FireAnim");
    21.         Fire();
    22.             }
    23.         }
    24.     }
    25.     else{
    26.     if(Input.GetButton("Fire1")){
    27.         if(AmmoLeft > 0){
    28.         BroadcastMessage("FireAnim");
    29.         Fire();
    30.             }
    31.         }
    32.     }
    33.    
    34.    
    35.     if(AmmoLeft == 0)
    36.     {
    37.         Reload();
    38.     }
    39.    
    40.     if(AmmoLeft < 0){
    41.         AmmoLeft = 0;
    42.     }
    43. }
    44.  
    45.  
    46. function Fire () {
    47.     audio.Play();
    48.     if(CanFire == true){
    49.     var Bullit = Instantiate(Bullet,Spawn.position,Spawn.rotation);
    50.         Bullit.rigidBody.AddForce(transform.foward * 1000);
    51.         SetCanFire();
    52.         AmmoLeft -= 1;
    53.     }
    54. }
    55.  
    56. function Reload(){
    57.     BroadcastMessage("ReloadAnim");
    58.     SetReload();
    59. }
    60.  
    61. function SetCanFire()
    62. {
    63.     CanFire = false;
    64.     yield WaitForSeconds(FireRate);
    65.     CanFire = true;
    66. }
    67.  
    68. function SetReload(){
    69. CanFire = false;
    70.     yield WaitForSeconds(ReloadTime);
    71.     CanFire = true;
    72. }
    73.    
    74.  
     
  8. bug5532

    bug5532

    Member

    Joined:
    Aug 16, 2011
    Messages:
    255
    you never add any more ammo...
     
  9. xxPillsxx

    xxPillsxx

    Member

    Joined:
    Oct 10, 2012
    Messages:
    21
    What do you mean?
     
  10. Loius

    Loius

    Member

    Joined:
    Aug 16, 2012
    Messages:
    537
    Try using StartCoroutine( FunctionName ). Some function chains silently fail to run coroutine functions if StartCoroutine isn't included (but including StartCoroutine won't cause any issues that I've noticed).
     
  11. bug5532

    bug5532

    Member

    Joined:
    Aug 16, 2011
    Messages:
    255
    well you tell it to reload when (AmmoLeft == 0), it then reloads. but as you dont have anything in the reload function like AmmoLeft = AmmoInMag, AmmoLeft will stay at 0, so next loop round it will just reload again.
    Change:
    function SetReload(){

    CanFire = false;

    yield WaitForSeconds(ReloadTime);

    CanFire = true;

    }
    to:
    function SetReload(){

    CanFire = false;

    yield WaitForSeconds(ReloadTime);
    AmmoLeft = AmmoInMag;
    CanFire = true;

    }
     
  12. xxPillsxx

    xxPillsxx

    Member

    Joined:
    Oct 10, 2012
    Messages:
    21
    Ahhh men the codes are all right,but it just won't work,I think I f*cked up something else not the code,I will send you my whole project file,do you want?
     
    Last edited: Oct 11, 2012
  13. xxPillsxx

    xxPillsxx

    Member

    Joined:
    Oct 10, 2012
    Messages:
    21
    I think you misunderstanded me,I mean that the gun never reloaded,and it fires rapidly even when I set FireRate to 10,not it's reloading forever lol
     
  14. All_American

    All_American

    Member

    Joined:
    Oct 14, 2011
    Messages:
    1,069
    Your using the reload function wrong. You don't just pause the firing to simulate reloading. You actually have to model the the bullets and clips in code to simulate the reloading .
     
  15. xxPillsxx

    xxPillsxx

    Member

    Joined:
    Oct 10, 2012
    Messages:
    21
    Meh,fixed,it's funny how I only missed a {
    LOL