统一和C#的新事物,(14,29)中的意外符号问题



在我的代码中,此错误资产/textchangescript.cs(14,29(:错误CS1525:意外符号(', expecting,', ;', or ='

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TextChangeScript : MonoBehaviour
{
    public Text m_MyText;
    public Text OtherText;
    void Start()
    {
        m_MyText.text = "There was once a mother and her child";
        yield WaitForSeconds (3);
        m_MyText.text = "The mother loved her child very dearly";
    }
}

您正在尝试在不返回IEnumerator的函数中调用yield WaitForSeconds。您需要创建一个返回IEnumerator的新功能,并使用StartCoroutine调用。

将执行yield之后的代码。

您可以检查文档。统一有充分的记录。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class WaitForSecondsExample : MonoBehaviour
{
    public Text m_MyText;
    public Text OtherText;

    void Start()
    {
        StartCoroutine(Example());
    }
    IEnumerator Example()
    {
        m_MyText.text = "There was once a mother and her child";
        yield return new WaitForSeconds(3);
        m_MyText.text = "The mother loved her child very dearly";
    }
}

使用coroutine,统一有充分的文献记录在其工作方式上,您可以在Chopi答案中使用链接。

现在避免任何混乱,通常在功能中,您希望返回成为您函数的最后一个调用,因为它背后的代码通常不会被调用。ienumerator并非如此。

该行yield return new WaitForSeconds(3);从函数返回表达式,并用作标记的位置,以在此行中3秒钟内执行执行。

您的开始方法不是ienumerator,就我跟踪而言,您的统一起初没有一种方法。您可以一开始就开始使用Coroutine,并具有该coroutine产量。这是一个coroutine的示例,它将为您提供,您的文本之间的3秒延迟:

using UnityEngine;
using UnityEngine.UI;
public class TextChangeScript : MonoBehaviour
{
    public Text m_MyText;
    public Text OtherText;
    IEnumerator StoryText() {
        m_MyText.text = "There was once a mother and her child";
        yield return new WaitForSeconds (3);
        m_MyText.text = "The mother loved her child very dearly";
        yield return new WaitForSeconds (3);
        m_MyText.text = "Then one day blah blah blah";
    }
    void Start()
    {
        StartCoroutine(StoryText());
    }
    void Update()
    {
    }
}

这是一个使用for loop循环的示例

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TextChangeScript : MonoBehaviour
{
    public Text m_MyText;
    public List<string> storyText;
    IEnumerator StoryText() {
        foreach (string sz in storyText)
        {
            text.text = sz;
            yield return new WaitForSeconds(3); // in 3 seconds. execution will begin here and iterate to the next string in the storyText.  
        }
    }
    void Start()
    {
        StartCoroutine(StoryText());
    }
    void Update()
    {
    }
}

编辑:

感谢R1Pfake在评论中共享的内容,您也可以执行此操作:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CenterName : MonoBehaviour {
    public Text text;
    public List<string> storyText;
    // Use this for initialization
    IEnumerator Start () {
        foreach (string sz in storyText)
        {
            text.text = sz;
            yield return new WaitForSeconds(3);
        }
    }
}

最新更新