How to change a material of primitive gameobject C#

1st time here. To begin, thank you all the help in advance.

I like to change a material of the primitive gameobject (script below). The gameObject is made in C# I like to know how to code the material for the PrimitiveType.Sphere. I have matRed variable which I have assigned with my own material in Unity. I want to add matRed onto my PrimiteType.Sphere, how to do that?

public Material matRed;
private IEnumerator SphereIndicator(Vector3 pos) {
		GameObject sphere = GameObject.CreatePrimitive (PrimitiveType.Sphere); = "Bullet";
		sphere.transform.position = pos;
		yield return new WaitForSeconds (1);
		Destroy (sphere);

Beginner =)

Hello there,

And welcome to Unity Answers!

To switch the material of a gameObject through script, you need to get a reference to its Renderer component first. To do that, you can use the line: sphere.GetComponent<Renderer>().material = matRed; .

I hope that helps!



Yes, that was easy for you. Thank you! =) Cheers, RBH