123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- using System.Collections.Generic;
- using UnityEngine;
- // Use Script in parent Folder of all humans
- public class ChangeMaterial : MonoBehaviour
- {
- public Material highlightRed;
- public Material highlightGreen;
- // Needed size, bc collision with box collider is checked
- private float radius = 1.0f;
- private Vector3 center;
- private Collider[] hitHumans;
- private List<Component> allHumansTransform = new List<Component>(); // only first depth children of parent (Humans)
- //#### If I include to parent. this should then be commented out ####
- // Start is called before the first frame update
- //private void Start()
- //{
- // Component[] allTransform = GetComponentsInChildren<Transform>(); // 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()
- {
- center = this.GetComponent<Transform>().position;
- hitHumans = Physics.OverlapSphere(center, radius, LayerMask.GetMask("Humans"));
- foreach (Collider hitHuman in hitHumans)
- {
- if(hitHuman.gameObject.name != gameObject.name)
- {
- Debug.Log(gameObject.name + " hit " + hitHuman.gameObject.name);
- if (hitHuman.GetComponent<MeshRenderer>().material != highlightRed)
- {
- hitHuman.GetComponent<MeshRenderer>().material = highlightRed;
- }
- else if (hitHuman.GetComponent<MeshRenderer>().material == highlightRed)
- {
- continue;
- }
- else
- {
- continue;
- }
- }
- }
- }
- }
|