统一和c#的新手,(14,29)中的意外符号问题

时间:2018-03-24 22:00:17

标签: c# unity3d

在我的代码中出现此错误 Assets / 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";
    }
}

2 个答案:

答案 0 :(得分:1)

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

yield之后的代码将被执行。

您可以查看documentation。 Unity已有详细记录。

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";
    }
}

答案 1 :(得分:1)

使用Coroutine,Unity会详细记录它们的工作方式,您可以使用Chopi答案中的链接。

现在为了避免任何混淆,通常在函数中你会希望返回是函数中的最后一个调用,因为它背后的代码通常不会被调用。对于IEnumerator,情况并非如此。

yield return new WaitForSeconds(3);从函数返回一个表达式,用作标记继续执行的位置,在这种情况下,该行在3秒内。

你的start方法不是IEnumerator,而且就我所追踪而言,在Unity的开始时没有办法产生。您可以在开始时启动协程并获得该协程的产量。这是一个协程的例子,它会给你,你的文字之间的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循环遍历文本的示例,它们之间有3秒的延迟:

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);
        }
    }
}