Changing colors available on the scene

Hello i have a ball that needs to change color in order to get past a colored path.
if the path is red the ball needs to be red in order to go through the path.
i wanted the player to be able to change the ball color according to the colors
available in the scene, if there is a red path but not a blue path, the blue color won’t be available.
is it possible to do something like this? and how? thanks

I think the first thing to do is to add a layer of separation. You’re not going to want to compare colors directly, as that can lead to floating point comparison issues. So instead, you’ll want to compare something that contains the color to another thing of the same kind, and then just use that color to display it. This might be an enum (useful if you want a dropdown in the inspector):

 public static class ColorManager {
public enum ColorEnum {Red, Green, Blue, Yellow};
public static Color GetColor(ColorEnum clr) {
if (clr == ColorEnum.Red) return Color.red;
// and so on
}
}

//on your Path class
public ColorEnum myColor = ColorEnum.Red;
[ExecuteInEditMode]
void Update(){ 
GetComponent<Renderer>().material.color = ColorManager.GetColor(myColor);
}

Then you’ll need to figure out which colors exist in your level. You’ll want a list of all your paths (maybe gotten via FindObjectsOfType), then loop through them. I’ll use a HashSet for this, which is basically just a list of things with fast lookup to see if a given value exists:

Path[] allPaths = FindObjectsOfType<Path>();
HashSet<ColorEnum> allColors = new HashSet<ColorEnum>();
for (int p=0;p<allPaths.Length;p++) {
if (!allColors.Contains(allPaths[p].myColor) ) allColors.Add(allPaths[p].myColor);
}
foreach (ColorEnum thisColor in allColors) {
//Initialize whatever you need to do per color that exists in your scene
}

Definitely possible, but there’s a lot of steps involved. Break your problem down into small pieces, and then try to figure out each piece.

  • You need a way to check the color of a path

  • You need a way to check the color of the ball

  • You need a way to make it so that the path blocks balls of the wrong color

  • You need a way to make a list of all the path colors in the current level

  • You need a way for the player to change the color of the ball. This probably has a bunch of substeps, like:

  • The player needs to express their intent to change the ball’s color

  • The game displays a list of available colors

  • The player chooses a color off of that list

Any time you’re not sure where to get started, try to break your current problem down into smaller pieces.