嗨,在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;
}
}
}
答案 0 :(得分:5)
该图像告诉您问题所在:您正在尝试在类型为GetComponent
而不是float
或GameObject
的字段上调用MonoBehaviour
。
floats
是类似于0.5
或2
的纯数字,它们没有组成部分。您可能需要将类型更改为GameObject
/ MonoBehaviour