2015-07-19 60 views
0

所以我使用Unity 3d遊戲引擎進行遊戲,我使用的是WaitForSeconds,但是我根據自己的改變一直運行着各種錯誤。這裏是我的代碼(不WaitForSeconds):WaitForSeconds不工作[c#]

using UnityEngine; 
using System.Collections; 

public class Attack : MonoBehaviour { 
    public int attack = 1; 
    public ParticleSystem MA; 

    // Use this for initialization 
    void Start() { 

     MA.enableEmission = false; 

    } 

    // Update is called once per frame 
    void Update() { 
     if(Input.GetKeyDown (KeyCode.E)){ 

      attack++; 
      if(attack > 3){ 

       attack = 1; 

      } 
      print (attack); 
     } 
     if(attack == 1){ 
      if(Input.GetKeyDown (KeyCode.Q)){ 

       print ("punch"); 


      } 
     } 

     if(attack == 2){ 

      if(Input.GetKeyDown (KeyCode.Q)){ 

       /*if(Input.GetKeyDown (KeyCode.Q)){ 
        WaitForSeconds magicdelay = new WaitForSeconds(1.0f); 
        yield return magicdelay; 

        /*MA.enableEmission = true; 
       yield return WaitForSeconds(1f); 
       MA.enableEmission = false; 

       }*/ 

       print ("magic"); 
       MA.enableEmission = true; 
       //I need help with the WaitForSeconds her 
      } 


      } 
     } 



    } 

我試過YieldInstruction WaitForSeconds = (int)1f;多次,但沒有奏效。請幫助!

回答

0
if(Input.GetKeyDown (KeyCode.Q)){ 

    StartCoroutine("Magic"); 

} 

IEnumerator Magic(){ 

    print ("magic"); 
    MA.enableEmission = true; 
    yield return new WaitForSeconds(1.0f); 
    MA.enableEmission = false; 

} 
0

這應該工作:

void Start() 
{ 
    StartCoroutine (MyCoroutine()); 
} 

IEnumerator MyCoroutine() 
{ 
    while(true) 
    { 
     Debug.Log(Time.time); 
     yield return new WaitForSeconds(1f); 
    } 
}