Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Set material of child object that entered the collider of another object.

Discussion in 'Scripting' started by danfiala23, Oct 28, 2019.

  1. danfiala23

    danfiala23

    Joined:
    Aug 31, 2019
    Posts:
    7
    Hi i need little help. I have this script:
    Code (CSharp):
    1.  
    2. Transform hex;
    3. public Material Dirt;
    4. public Renderer[] childColors;
    5.  
    6. public void Start()
    7. {
    8.    
    9. }
    10. void OnTriggerEnter(Collider other)
    11. {
    12.    
    13.    
    14.      if (other.gameObject.tag == "Grass")
    15.      {
    16.        
    17.          //MeshRenderer childColors = other.GetComponentInChildren<MeshRenderer>();
    18.          //hex = other.transform.GetChild(0).GetChild(0);
    19.          foreach (Renderer color in childColors)
    20.          {
    21.              color.material = Dirt;
    22.          }
    23.      }
    24. }
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Hmm. At first blush, you really should not name a renderer 'color'. It really throws People of who work with renderers and/or Colors.

    The Problem in your code above is that your foreach code always looks at 'childcolors' array, but never updates it. So you are assigning the Dirt material to the renderers in childColors over and over again. From the (much too short) description of what you are trying to accomplish I guess you should access the renderers of the collider 'other'
     
  3. danfiala23

    danfiala23

    Joined:
    Aug 31, 2019
    Posts:
    7
    Yeah