WaitForSeconds不工作[c#]

时间:2015-07-19 18:57:57

标签: c# unity3d

所以我正在使用 Unity 3d 游戏引擎制作游戏,而我正在使用WaitForSeconds,但我会根据改变的内容遇到各种错误。这是我的代码(没有WaitForSeconds):

    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;,但这不起作用。求救!

3 个答案:

答案 0 :(得分:0)

你必须使用Unity3d协程来解决问题。

有一些有用的链接可以提供帮助:

  1. Unity3D coroutine class
  2. How to start Unity3D coroutines

答案 1 :(得分:0)

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

   StartCoroutine("Magic");

}

IEnumerator Magic(){

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

}

答案 2 :(得分:0)

这应该有效:

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

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