Unity Community


Results 1 to 15 of 15

  1. Posts
    14

    yield WaitForSeconds not working

    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:  
    1. function Fire () {
    2.     audio.Play();
    3.     if(CanFire == true){
    4.     var Bullit = Instantiate(Bullet,Spawn.position,Spawn.rotation);
    5.         Bullit.rigidBody.AddForce(transform.foward * 1000);
    6.         CanFire = false;
    7.         yield WaitForSeconds(FireRate);
    8.         CanFire = true;
    9.         AmmoLeft -= 1;
    10.     }
    11. }
    12.  
    13. function Reload(){
    14.     BroadcastMessage("ReloadAnim");
    15.     CanFire = false;
    16.     yield WaitForSeconds(ReloadTime);
    17.     CanFire = true;
    18. }
    Code:  
    1. var Bullet : Transform;
    2. var Spawn : Transform;
    3. var BulletSpeed : float = 1000;
    4. var ReloadTime : float = 2;
    5. var AmmoInMag : float = 30;
    6. var IsFullAuto = true;
    7. static var AmmoLeft : float;
    8. private var CanFire = true;
    9. var FireRate = 0.8;


  2. Location
    Metro-Detroit
    Posts
    1,068
    I don't see the reload function being called anywhere.
    Last edited by All_American; 10-10-2012 at 08:19 AM.


  3. Posts
    14
    Quote Originally Posted by All_American View Post
    Code:  
    1. /
    2. /
    3. public float BlahTime = 2.0f;
    4.  
    5.  void start/update()
    6.     {          
    7.                      Blah1();      
    8.     }
    9.   void Blah1()
    10.     {      
    11.    StartCoroutine(Blah2());
    12.         }
    13.     IEnumerator Blah2()
    14.         {          
    15.         yield return new WaitForSeconds(BlahTime);
    16.         {
    17.             Blah = Blah blah blah;
    18.         }
    19.     }
    20. /
    21. /
    But..I'm using Javascript...D:


  4. Location
    Metro-Detroit
    Posts
    1,068
    You have to call the reload function somewhere 'reload();"

    I am not seeing that in your code.

    Like

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


  5. Posts
    14
    Quote Originally Posted by All_American View Post
    You have to call the reload function somewhere 'reload();"

    I am not seeing that in your code.

    Like

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


  6. Location
    Metro-Detroit
    Posts
    1,068
    Is line 20 in your reload function??? or fire function??


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


  8. Location
    UK
    Posts
    247
    you never add any more ammo...
    GnarBike Trials - Motorbike game, build and share levels online!


  9. Posts
    14
    Quote Originally Posted by bug5532 View Post
    you never add any more ammo...
    What do you mean?


  10. Location
    Yesterday
    Posts
    507
    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. Location
    UK
    Posts
    247
    Quote Originally Posted by xxPillsxx View Post
    What do you mean?
    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;

    }
    GnarBike Trials - Motorbike game, build and share levels online!


  12. Posts
    14
    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 by xxPillsxx; 10-11-2012 at 03:27 AM.


  13. Posts
    14
    Quote Originally Posted by bug5532 View Post
    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;

    }
    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. Location
    Metro-Detroit
    Posts
    1,068
    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. Posts
    14
    Meh,fixed,it's funny how I only missed a {
    LOL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •