ChangeMaterial.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. // Use Script in parent Folder of all humans
  4. public class ChangeMaterial : MonoBehaviour
  5. {
  6. public Material highlightRed;
  7. public Material highlightGreen;
  8. // Needed size, bc collision with box collider is checked
  9. private float radius = 1.0f;
  10. private Vector3 center;
  11. private Collider[] hitHumans;
  12. private List<Component> allHumansTransform = new List<Component>(); // only first depth children of parent (Humans)
  13. //#### If I include to parent. this should then be commented out ####
  14. // Start is called before the first frame update
  15. //private void Start()
  16. //{
  17. // Component[] allTransform = GetComponentsInChildren<Transform>(); // all possible children of parent object
  18. // foreach(Transform child in allTransform)
  19. // {
  20. // if(child.parent == allTransform[0])
  21. // {
  22. // allHumansTransform.Add(child);
  23. // }
  24. // }
  25. // foreach(Component lst in allHumansTransform)
  26. // {
  27. // Debug.Log(lst.gameObject.name);
  28. // }
  29. //}
  30. private void Update()
  31. {
  32. center = this.GetComponent<Transform>().position;
  33. hitHumans = Physics.OverlapSphere(center, radius, LayerMask.GetMask("Humans"));
  34. foreach (Collider hitHuman in hitHumans)
  35. {
  36. if(hitHuman.gameObject.name != gameObject.name)
  37. {
  38. Debug.Log(gameObject.name + " hit " + hitHuman.gameObject.name);
  39. if (hitHuman.GetComponent<MeshRenderer>().material != highlightRed)
  40. {
  41. hitHuman.GetComponent<MeshRenderer>().material = highlightRed;
  42. }
  43. else if (hitHuman.GetComponent<MeshRenderer>().material == highlightRed)
  44. {
  45. continue;
  46. }
  47. else
  48. {
  49. continue;
  50. }
  51. }
  52. }
  53. }
  54. }