C# errors in Unity

前端 未结 1 971
旧巷少年郎
旧巷少年郎 2021-01-29 05:59

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 th

相关标签:
1条回答
  • 2021-01-29 06:43

    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;
         }
     }
    
    0 讨论(0)
提交回复
热议问题