GazeProject/Assets/EYE ADVANCED/Scripts/LightColorRandomizer.cs
2025-05-15 15:19:18 +02:00

76 lines
2.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LightColorRandomizer : MonoBehaviour
{
[SerializeField] private GameObject[] allObjects; // All 9 objects
[SerializeField] private int greenObjectsCount = 3; // Number of objects to start as green
[SerializeField] private Material greenMaterial; // Material for green state
[SerializeField] private Material redMaterial; // Material for red state
// Use Awake instead of Start to avoid conflicts
void Awake()
{
// Randomize which objects are green
RandomizeColors();
}
public void RandomizeColors()
{
if (allObjects.Length == 0)
{
Debug.LogError("No objects assigned to LightColorRandomizer!");
return;
}
// Create a list of all object indices
List<int> indices = new List<int>();
for (int i = 0; i < allObjects.Length; i++)
{
indices.Add(i);
}
// Shuffle the list
ShuffleList(indices);
// Take the first 'greenObjectsCount' indices and set those objects to green, the rest to red
for (int i = 0; i < allObjects.Length; i++)
{
GameObject obj = allObjects[i];
EyeUnlockSequence sequence = obj.GetComponent<EyeUnlockSequence>();
if (sequence == null)
{
Debug.LogWarning($"Object {obj.name} does not have an EyeUnlockSequence component.");
continue;
}
// Set the first 'greenObjectsCount' objects to green
bool shouldBeGreen = i < greenObjectsCount;
sequence.IsGreen = shouldBeGreen;
// Also assign the materials directly to make sure they're set correctly
sequence.SetMaterials(greenMaterial, redMaterial);
// Force an immediate material update
sequence.UpdateMaterialBasedOnState();
Debug.Log($"Set {obj.name} to {(shouldBeGreen ? "Green" : "Red")}");
}
}
private void ShuffleList<T>(List<T> list)
{
int n = list.Count;
while (n > 1)
{
n--;
int k = Random.Range(0, n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
}