Unity中的C#错误 [英] C# errors in Unity

查看:448
本文介绍了Unity中的C#错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到脚本错误.但是首先我要做什么.

I'm having script errors. But first what I'm doing.

我要使文本色彩丰富,系统应如下所示.每秒都有新的差异色.但是我编写了代码脚本,并显示了错误.喜欢:

I'm making my text colorful, the system should look like this. Every second, new differet color. But I scripted the code and it shows errors. Like:

错误CS0619: UnityEngine.Component.renderer' is obsolete:属性渲染器已被弃用.使用GetComponent()代替. (UnityUpgradable)'

error CS0619: UnityEngine.Component.renderer' is obsolete:Property renderer has been deprecated. Use GetComponent() instead. (UnityUpgradable)'

错误CS1061 :类型为UnityEngine.Component' does not contain a definition for material',找不到扩展方法material' of type UnityEngine.Component'(您是否缺少using指令或程序集引用?)

error CS1061: Type UnityEngine.Component' does not contain a definition formaterial' and no extension method material' of typeUnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)

脚本也是这个.

using UnityEngine;
using System.Collections;

public class Colors : MonoBehaviour

{
    public float timer = 0.0f;

    void Start()
    {

    }


    void Update()
    {
        timer += Time.deltaTime;
        if (timer >= 2.0f)//change the float value here to change how long it takes to switch.
        {
        // pick a random color
            Color newColor = new Color(Random.value, Random.value, Random.value, 1.0f);
        // apply it on current object's material
            renderer.material.color = newColor;
            timer = 0;
        }
    }
}

推荐答案

从Unity 5开始,您将无法再直接访问renderer.material.color.您必须先使用GetComponent<Renderer>();获取GameObject的组件,然后才能从Renderer访问材料.

You can no longer access renderer.material.color directly anymore starting from Unity 5 up. you must use GetComponent<Renderer>(); to get the component of the GameObject first, then you can access the material from the Renderer.

public float timer = 0.0f;
 Renderer rd;

 void Start()
 {
     rd = gameObject.GetComponent<Renderer>();
 }


 void Update()
 {
     timer += Time.deltaTime;
     if (timer >= 2.0f)//change the float value here to change how long it takes to switch.
     {
         // pick a random color
         Color newColor = new Color(Random.value, Random.value, Random.value, 1.0f);
         // apply it on current object's material
         rd.material.color = newColor;

         timer = 0;
     }
 }

这篇关于Unity中的C#错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆