This is my solution
I communicate between SpawnNewBlock.CS and ColorChanger.CS so I can choose a random color from Enum. ( I’m using “_CurrenColer = (Colors)randomValue;” so i can get the color from enum using values)
[SpawnNewBlock.Cs]
using UnityEngine;
public class SpawnNewBlock : MonoBehaviour
{
[SerializeField] GameObject blockPrefab;
[SerializeField] Transform spawnPosition;
private GameObject[] currentBlock = new GameObject[100000];
private int blockIndex = 0;
public void SpawnBlock()
{
currentBlock[blockIndex] = Instantiate(blockPrefab, spawnPosition.position, Quaternion.identity);
currentBlock[blockIndex].GetComponent<ColorChanger>().RandomColors();
blockIndex++;
}
[ColorChanger.CS]
using UnityEngine;
public class ColorChanger : MonoBehaviour
{
private SpriteRenderer mySpriteRenderer;
private Color myColor;
void Awake()
{
mySpriteRenderer = GetComponent<SpriteRenderer>();
}
private enum Colors
{
Yellow = 1,
Cyan = 2,
Red = 3
}
[SerializeField]
private Colors _CurrenColer;
private void Start()
{
switch (_CurrenColer)
{
case Colors.Yellow:
myColor = Color.yellow;
break;
case Colors.Cyan:
myColor = Color.cyan;
break;
case Colors.Red:
myColor = Color.red;
break;
}
mySpriteRenderer.color = myColor;
}
public void RandomColors()
{
// Random Enum Value
int randomValue = Random.Range(1, 4);
// Random Colors
_CurrenColer = (Colors)randomValue;
}
}