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;
}
}
}