I Need help to access random value of a rendered Material selected.

I can see the value using Debug.Log but cannot access that value within code.

I used 4 materials that I called Red, Black, Blue, Orange in a list to change the color of a circle using a random code Rend.sharedMaterial = material [Random.Range (0,4); That work just find
I can see the name of the material selected using Debug.Log (Rend shareMaterial) which reads out as Black (UnityEngine. material).
But do not know how to write the code to that access that name (Black, Red, Blue or Orange). I tried various methods that I would have used in other languages but with no luck.
I am sure it is simple but I really need someone to show me how.

If all you need to know is which material is being used (e.g. because different colored circles behave differently, but not differently enough to make separate classes for), there’s several solutions to this; one of these solutions to is to use an Enumerator.

Firstly, you’ll need a script to put on the circle objects if they don’t already have one, Secondly, define this enum, either in your circle object’s script, or in its own file:

public enum CircleType
{
	RED,
	BLACK,
	BLUE,
	ORANGE
};

Now, declare a CircleType variable in your circle object’s script:

public CircleType circleType;

Then, whenever you set the material, also set the circle’s circle type (e.g. to CircleType.ORANGE, etc). When you need to know which material the circle has, get the circleType from the circle object’s script. You can read more about enums at microsoft’s C# reference pages.

This isn’t necessarily the safest solution, and it can be prone to programmer error if you’re not careful about ensuring the enum type matches up with the material variant whenever you set one or the other. If this doesn’t solve your problem, we’ll need more details about what you’re trying to do with these circles and why you need to know which material the circle has.

A side note: If the only difference between the Materials is the color, instead of making separate Materials for each circle type, you can just set the color of the circle’s SpriteRenderer instead.

I solve the problem. Thank you for your suggestions

I used this line of code

if (collider.gamObject.tag == “Blue”" && rend.sharedMaterial == material [(3)]