- Home /
How to detect if three different colors have been selected
I have three objects which change color when the player clicks on each one. I want the player to guess and choose in which order he wants the colors Red, Yellow and Blue for each object so that a cube is opened. The goal is for the player to click on objects and stop when one of them is red, the other blue and the other yellow. Does anyone know if this is possible and how can it be done? Thank you!
Ηere is the code with which each object changes color. It works well using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Linq;
//Make sure to change the class name (CCSphere) to whatever you called your script.
public class ColorManager : MonoBehaviour
{
int colorNumber;
public Color[] ObjectColors;
void Awake()
{
ObjectColors = new Color[]
{
Color.red,
Color.yellow,
Color.blue,
};
colorNumber = 0;
}
void OnMouseDown()
{
ChangeColor(-1);
}
void ChangeColor(int specificColor)
{
if (specificColor < 0)
{
this.GetComponent<MeshRenderer>().material.color = ObjectColors[colorNumber++];
if (colorNumber >= ObjectColors.Length)
colorNumber = 0;
}
else
{
this.GetComponent<Renderer>().material.color = ObjectColors[specificColor];
colorNumber = specificColor + 1;
if (colorNumber >= ObjectColors.Length)
colorNumber = 0;
}
}
}
----------
And here I found on web another code that does just the opposite. It opens a cube if the colors are the same. I tried to modify it but I could not.
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class CylindersBlackColor : MonoBehaviour
{
public GameObject[] Cylinders;
public GameObject Cube;
private List<Renderer> _rendererCache;
private void Start()
{
// Fill up renderer cache
_rendererCache = new List<Renderer>();
foreach (var cube in Cylinders)
_rendererCache.Add(cube.GetComponent<Renderer>());
}
private void Update()
{
if (_rendererCache.All(f => f.material.color == Color.red || f.material.color == Color.yellow || f.material.color == Color.blue) && Cube != null)
{
Destroy(Cube);
Cube = null;
}
}
}
Answer by paulsz · Dec 08, 2021 at 07:55 AM
If I understood your problem well, I wrote 2 scripts that should do what you want. Let me know if this helps:
Put this script on every cylinder:
using UnityEngine;
public class ColorOrderDetection : MonoBehaviour
{
private Color[] colorOrder = new Color[] { Color.yellow, Color.blue, Color.red };
[SerializeField] private ColorSelectionCylinder[] cylinders;
internal void CheckIfColorsAreCorrect()
{
for (int i = 0; i < colorOrder.Length; i++)
{
if (cylinders[i].Color != colorOrder[i])
{
Debug.Log("No color match yet");
return;
}
}
Debug.Log("Color Matched !");
}
}
Then place a manager object with this script:
using UnityEngine;
public class ColorSelectionCylinder : MonoBehaviour
{
private static readonly Color[] colors = new Color[] { Color.red, Color.yellow, Color.blue };
private int colorIndex = 0;
[SerializeField] private ColorOrderDetection colorOrderDetection;
private MeshRenderer mr;
public Color Color => mr.material.color;
private void Awake()
{
mr = GetComponent<MeshRenderer>();
}
private void OnMouseDown()
{
mr.material.color = colors[colorIndex];
colorIndex++;
if (colorIndex >= colors.Length)
{
colorIndex = 0;
}
colorOrderDetection.CheckIfColorsAreCorrect();
}
}
Set the correct references in scene and it should do what I understood you wanted :)
Answer by ioannapnl · Dec 08, 2021 at 01:57 PM
@paulsz Thank you for your answer and help but unfortunately although I followed what you told me it did not work. no message from debug.log or error appears. Maybe i didn't set the correct references in scene. I' ll keep trying. Here I have a video to make my problem more understandable. link text
Your answer
