using System.Collections.Generic; using UnityEngine; // Use Script in parent Folder of all humans public class ChangeCapsuleMaterial : MonoBehaviour { public Material highlightRed; public Material highlightGreen; public Material highlightYellow; public int threshold = 1; public float radius = 1f; private Vector3 center; private Collider[] hitHumans; private List allHumansTransform = new List(); // only first depth children of parent (Humans) // Start is called before the first frame update private void Start() { Component[] allTransform = GetComponentsInChildren(); // all possible children of parent object foreach (Transform child in allTransform) { if (child.parent == allTransform[0]) { allHumansTransform.Add(child); } } //foreach (Component lst in allHumansTransform) //{ // Debug.Log(lst.gameObject.name); //} } private void Update() { foreach (Component lst in allHumansTransform) { center = lst.GetComponent().position; // only collisions with Layer "Humans" are listed in "hitHumans" hitHumans = Physics.OverlapSphere(center, radius, LayerMask.GetMask("Humans")); // if Humans only hits themselves (threshold == 1) or nobody, then Material change to Green if (hitHumans.Length <= 1) { Debug.Log(lst.gameObject.name + " set to Green"); lst.GetComponent().material = highlightGreen; } // if Human hits lesser then threshold but is hitting minimum 1 other human, then Material change to Yellow else if (hitHumans.Length <= threshold) { Debug.Log(lst.gameObject.name + " set to Yellow"); lst.transform.GetChild(0).GetChild(0).gameObject.GetComponent().material = highlightYellow; } else { // if Humans hits somebody else (threshold >= 1), then Material change to Red foreach (Collider hitHuman in hitHumans) { if (lst.gameObject.name != hitHuman.gameObject.name) // overlap with themselves { if (hitHuman.GetComponent().material != highlightRed || lst.GetComponent().material != highlightRed) { Debug.Log(lst.gameObject.name + " overlaps with " + hitHuman.gameObject.name); hitHuman.GetComponent().material = highlightRed; lst.GetComponent().material = highlightRed; } } } } } } }