Search Unity

Get all materials with tag, best way?

Discussion in 'Scripting' started by uk, Oct 18, 2013.

  1. uk

    uk

    Joined:
    Apr 8, 2013
    Posts:
    14
    Alright, the following code should work, but it just seem alot of code for something quite simple, is there any simpler way of doing this?

    Code (csharp):
    1. public Material[] dots;
    2.  
    3.     void Awake()
    4.     {
    5.         dots = new Material[4];
    6.        
    7.         Renderer[] rendererdots = GameObject.FindWithTag("dots").GetComponentsInChildren<Renderer>();
    8.  
    9.         int i = 0;
    10.         foreach(Renderer rndr in rendererdots )
    11.         {
    12.             dots[i] = rndr.material;
    13.             i++;
    14.         }
    15.      }