协程不能正确唤醒

时间:2019-02-19 12:15:12

标签: c# unity3d coroutine oculus

嗨,在C#中我的脚本的以下协程部分遇到了麻烦。我收到的错误消息附在屏幕抓取中,似乎是:GetComponent<Renderer>()

using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;

public class test: MonoBehaviour
{
    public float model2;
    public float model1;

    public void section1()
    {
        SceneManager.LoadScene("section1", LoadSceneMode.Single);
    }

    public void fade()
    {
       StartCoroutine(colorlerpin());
    }

    public IEnumerator colorlerpin()
    {    
        float ElapsedTime2 = 0.0f;
        float TotalTime2 = 1f;
        while (ElapsedTime2 < TotalTime2)
        {
            //  fades out main heart
            ElapsedTime2 += Time.deltaTime;
            model1.GetComponent<Renderer>().material.color = Color.Lerp(new Color(1f, 1f, 1f, 1f), new Color(1f, 1f, 1f, 0f), (ElapsedTime2 / TotalTime2));
            yield return null;
            //  fades in cutaway
            ElapsedTime2 += Time.deltaTime;
            model2.GetComponent<Renderer>().material.color = Color.Lerp(new Color(1f, 1f, 1f, 0f), new Color(1f, 1f, 1f, 1f), (ElapsedTime2 / TotalTime2));
            yield return null;
        }
    }
}

enter image description here

1 个答案:

答案 0 :(得分:5)

该图像告诉您问题所在:您正在尝试在类型为GetComponent而不是floatGameObject的字段上调用MonoBehaviour

floats是类似于0.52的纯数字,它们没有组成部分。您可能需要将类型更改为GameObject / MonoBehaviour