c#Script utube示例中的错误

时间:2018-02-22 14:23:16

标签: c# unity3d

我一直在关注一个utube示例并且已经卡住了,我试图让一个块消失,当玩家头部挡住了我已经被给了代码但是它没有工作。我收到以下错误

  

Assets / Scripts / BlockDestroy.cs(15,4):错误CS1622:无法从迭代器返回值。使用yield return语句返回一个值,或者使用yield break来结束迭代

这是我的代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BlockDestroy : MonoBehaviour {

    public float xPos; 
    public float yPos; 
    public float zPos; 
    float waiting = 0.02F; 

    IEnumerator OnTriggerEnter(Collider col) { 
        if (col.gameObject.tag == "Player") { 
            this.transform.position = new Vector3(xPos, yPos + 0.1F, zPos); 
            return new WaitForSeconds(waiting); 
            this.transform.position = new Vector3(xPos, yPos + 0.2F, zPos); 

            yield return new WaitForSeconds(waiting); 
            transform.GetComponent<Collider>().isTrigger = false; 
            this.transform.position = new Vector3(xPos, yPos + 0.3F, zPos + 0.5F); 

            yield return new WaitForSeconds(waiting); 
            this.transform.position = new Vector3(xPos, yPos + 0.4F, zPos + 1.0F); 

            yield return new WaitForSeconds(waiting); 
            this.transform.position = new Vector3(xPos, yPos - 0.1F, zPos + 1.5F); 

            yield return new WaitForSeconds(waiting); 
            this.transform.position = new Vector3(xPos, yPos - 0.6F, zPos + 2.0F); 

            yield return new WaitForSeconds(waiting); 
            this.transform.position = new Vector3(xPos, yPos - 1.6F, zPos + 2.0F); 

            yield return new WaitForSeconds(waiting); 
            this.transform.position = new Vector3(xPos, yPos - 2.6F, zPos + 2.0F); 

            yield return new WaitForSeconds(waiting); 
            this.transform.position = new Vector3(xPos, yPos - 4.0F, zPos + 2.0F); 

            yield return new WaitForSeconds(0.25F); 
            transform.GetComponent<Collider>().isTrigger = true; 
            Destroy(gameObject); 

        } 
    } // Use this for initialization 

    void Start() {
        xPos = transform.position.x;
        yPos = transform.position.y;
        zPos = transform.position.z;
    } 
}

1 个答案:

答案 0 :(得分:2)

您忘记了以下行中的yield关键字:

if (col.gameObject.tag == "Player") { 
    this.transform.position = new Vector3(xPos, yPos + 0.1F, zPos); 
    //return new WaitForSeconds(waiting); // ← HERE
    yield return new WaitForSeconds(waiting); // CORRECT
    this.transform.position = new Vector3(xPos, yPos + 0.2F, zPos);