There is no 'Material' attached to the "CapitalCube" game object, but a script is trying to access i

phys_capitalCube.GetComponent<Material>().color = newCubeColor;

This line throws following error.

A script Instantiates a prefab, that prefab is a City, City has a script (line above is in this script), it has a child called CapitalCube. Weirdly enough I thought each GameObject, no matter how simple always has a material (and more when it’s a standard cube). I tried including Material but as expected it’s not a component I can include.

It can’t be an issue of missing reference, I would get NullReferenceError. Everything succeeds up until the point it reaches GetComponent<<Material>(), that’s when it throws.

So, how can I help you help me solve this issue?

PS: Yes, I definitely first Instantiate and then get material from that cube. Function that Instantiates later tries changing that material color.
PS2: Yes, CapitalCube is assigned in the Inspector.

Material isn’t a component. Material is a property of a Renderer; the Renderer is the component.

6 Likes