Accessing Script Applied on a prefab on runtime

Hello there!

So I have come across a problem that I cannot solve.

I have A script attached to an prefab called “Whooly Rhino”.
So far this main script is used for random movements across the 3D plane.

Attached to this plane I have a “World” Script that initializes 6 different instances of the rhino.
These different instances are split into 2 separate groups called red group and blue group with a new script that i apply upon initialization.

//Group 1 
     for (i = 0; i < 3; i++)
            {
                GameObject WhoolyRhino = (GameObject)Instantiate(prefab, new Vector3(i * 2.0f, 0, 0), Quaternion.identity);
                WhoolyRhino.name = "WhoolyRhino_" + i;
                WhoolyRhino.AddComponent<RedGroup>();
                WhoolyRhinos *= WhoolyRhino;*

}

//Group 2
for (i = 3; i < 6; i++)
{

GameObject WhoolyRhino = (GameObject)Instantiate(prefab, new Vector3(i * 1.0f, 0, 0), Quaternion.identity);
WhoolyRhino.name = “WhoolyRhino_” + i;
WhoolyRhino.AddComponent();
WhoolyRhinos = WhoolyRhino;
}
I would like for the random movement script to let me check if the object which contains that script has either the blue or red script attached to it and fetch the variable “Color” from the below code:
public class BlueGroup : MonoBehaviour {

public string color = “blue”;
public float test;
I know that we can access scripts based on name, but I am not sure how to set up a prefab so that it modifies code inside of it dependent on name.
Because an ideal solution would be for my random movement script to have code changed inside of it based on whatever “WhoolyRhino.name = " " + i;” is equal to so that i can call that object and therefor find which script is attached.
Many thanks,
Jake

To check and access a component to a GameObject use that:

string color;
if(gameObject.GetComponent<BlueGroup>() !=null)
{
         color = gameObject.GetComponent<BlueGroup>().color;
}else if(gameObject.GetComponent<RedGroup>() !=null){
         color = gameObject.GetComponent<RedGroup>().color;
}else
        Debug.Log("No group script found");

of course it would be massively more efficient to have a generic script instead of 2 color groups like

public class Group : MonoBehaviour{
        public GroupColor groupColor;
        
        public enum GroupColor{ red , blue};
}

set the color in that class and access it with:

string color = GetComponent<Group>().groupColor.ToString();