Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

WaitForSeconds Damage PowerUp

Discussion in 'Scripting' started by JoeCBS, Jun 26, 2014.

  1. JoeCBS

    JoeCBS

    Joined:
    Feb 25, 2013
    Posts:
    41
    Hey there everyone. I'm a bit confused on how to use WaitForSeconds. I am trying to have an OnTriggerEnter add damage to my gun, but for only 15 seconds since it is a PowerUp. I ahve tried making two methods one ExectueWait and One the IEnumerator Wait for the actual WaitForSeconds. It just automatically ended the wait, even though I have it set for 20 seconds. Going to post code with my attempt in it.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DamagePowerUpTrigger : MonoBehaviour {
    5.  
    6.     private int num = 3;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.    
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.    
    16.     }
    17.  
    18.     public void OnTriggerEnter(Collider other)
    19.     {
    20.         Debug.Log ("Entered Collider!" + gameObject.name);
    21.         if(other.gameObject) {
    22.             if(gameObject.name.StartsWith("DamagePowerUp")){
    23.                 PlayerController playerController = other.GetComponent<PlayerController>();
    24.                 if(playerController == null) return;
    25.                 Debug.Log ("Nulled");
    26.                 if(playerController.currentGun == null) return;
    27.                 Debug.Log ("CurrentGun Nulled");
    28.                 if(playerController.currentGun == playerController.machinegun) {
    29.                     Debug.Log ("Entered");
    30.                     playerController.currentGun.damage += 4;
    31.                     executeWait();
    32.                     playerController.currentGun.damage =1;
    33.                     GameObject.Destroy(gameObject);
    34.                 }
    35.                 /*else if(playerController.currentGun == playerController.shotgun) {
    36.                     Debug.Log ("Entered");
    37.                     playerController.currentGun.totalAmmo += 10;
    38.                     GameObject.Destroy(gameObject);
    39.                 }
    40.                 else if(playerController.currentGun == playerController.pistol) {
    41.                     Debug.Log ("Entered Pistol");
    42.                     playerController.currentGun.totalAmmo += 10;
    43.                     GameObject.Destroy(gameObject);
    44.                 }*/
    45.             }
    46.         }
    47.     }
    48.  
    49.     private void executeWait()
    50.     {
    51.         Debug.Log("wait start");
    52.         StartCoroutine(Wait(20.0f));
    53.         Debug.Log("This gets called after starting the Coroutine");
    54.        
    55.     }
    56.    
    57.     private IEnumerator Wait(float seconds)
    58.     {
    59.         Debug.Log("waiting");
    60.         yield return new WaitForSeconds(seconds);
    61.         Debug.Log("wait end");
    62.     }
    63. }
    64.  
     
  2. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    You need to put
    Code (CSharp):
    1.                   playerController.currentGun.damage =1;
    2.                GameObject.Destroy(gameObject);
    Behind your WaitForSeconds inside your Coroutine.

    The OnTriggerEnter function is not waiting for your Wait function end.

    This official Video Tutoreal will help you to understand the concept of Coroutines.
     
  3. JoeCBS

    JoeCBS

    Joined:
    Feb 25, 2013
    Posts:
    41

    If I put those behind the 2 lines, woudn't it set damage to 4, then 1, then destroy, then wait?