- Home /
[CLOSED]Unity 5 GameObject.Renderer.Material
Did something changed on Unity 5 ?
I know how to do this another ways but I don't know why this is not working:
I'm trying to change the GameObject color using
gameObject.renderer.material
but it gives error : "error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement"
**EDIT: UNITY changed this, now to use 'gameObject.renderer.material' you need to change to
gameObject.GetComponent<Renderer>().material
Thats my entire code:
using UnityEngine;
using System.Collections;
public class RiftRay : MonoBehaviour {
public LayerMask mask;
public GameObject[] pisos;
public int distance;
void Update ()
{
Vector3 fwd = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.position, fwd, Color.green);
if (Physics.Raycast(transform.position, fwd, distance ,mask ))
{
print("a");
pisos[0].renderer.material.color = Color.black;
}
}
}
Answer by tanoshimi · Feb 24, 2015 at 07:45 PM
That's nothing to do with Unity 5; that's just bad code. Line 19 should read:
pisos[0].renderer.material.color = Color.black;
Sorry, was my bad now, the error is: error CS0619: UnityEngine.GameObject.renderer' is obsolete:
Property renderer has been deprecated. Use GetComponent() ins$$anonymous$$d. (UnityUpgradable)'
I changed to pisos[0].GetComponent().material.color = Color.black; and worked
As others have noted, this is no longer a valid answer. Everyone should look at the answers below ins$$anonymous$$d.
Answer by cesarureno · Mar 17, 2015 at 08:28 AM
GetComponent<Renderer>().material.color = Color.black;
Try this
Answer by TrollAxeThrower · Sep 16, 2015 at 03:36 PM
cesarureno's answer didn't work for me. just this one:
GetComponent<Renderer>().materials[0].color = Color.Black;