简体   繁体   English

Unity5中的WaitForSeconds错误

[英]WaitForSeconds Error in Unity5

I am trying to set up a simple script where if I collide with an object (just a simple box for now) then it triggers text to show on the screen with the "zone" name. 我试图建立一个简单的脚本,如果我与一个对象(现在只是一个简单的盒子)发生碰撞,那么它将触发文本以“区域”名称显示在屏幕上。

I'm getting a script error and I can't seem to figure out what it's telling me to do. 我遇到了脚本错误,但似乎无法弄清楚它告诉我要做什么。 I've tried to look at various tutorials but what keeps coming up is something static like a player name or score. 我尝试看过各种教程,但是不断出现的是一些静态信息,例如球员姓名或得分。

I want the text to fade after a bit, hence the extra function at the end that I am trying to pass the zone name to. 我希望文本稍稍褪色,因此在尝试将区域名称传递到末尾时具有额外的功能。 I am attaching this script to each zone trigger. 我将此脚本附加到每个区域触发器。

Here's my code: 这是我的代码:

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

public class ZoneAnnounce : MonoBehaviour {

    // Text Object
    private Text zoneNameText;

    // Exposed Text Variable
    public string zoneName = "Unknown";

    void OnCollisionEnter (Collision col) {

        if ( col.gameObject.name == "Player" ) {
            StartCoroutine(showZoneInfo(zoneName));
        }

    }   

    IEnumerator showZoneInfo (string zoneName) {

        zoneNameText.text = zoneName;

        yield return WaitForSeconds(3);

    }

}

and here's the error I am getting: 这是我得到的错误:

Assets/ZoneAnnounce.cs(26,16): error CS0119: Expression denotes a type, where a variable, value or method group was expected Assets / ZoneAnnounce.cs(26,16):错误CS0119:表达式表示一种类型,其中应有变量,值或方法组

The problem is here: yield return WaitForSeconds(3); 问题在这里: yield return WaitForSeconds(3);

WaitForSeconds is class . WaitForSecondsclass To yield it you have to create new instance of it. 要产生它,您必须创建它的新实例。 This can be done by simply adding the new keyword before WaitForSeconds . 只需在WaitForSeconds之前添加new关键字即可完成此操作。

Change that to yield return new WaitForSeconds(3); 更改以yield return new WaitForSeconds(3);


If you already know the time(3 seconds) to wait, you can create new instance of WaitForSeconds then use it without the new keyword. 如果您已经知道等待时间(3秒),则可以创建新的WaitForSeconds实例,然后在不使用new关键字的情况下使用它。

WaitForSeconds waitTime = new WaitForSeconds(3);
IEnumerator showZoneInfo(string zoneName)
{

    zoneNameText.text = zoneName;
    yield return waitTime;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM