/// <summary> /// Assign a randomly chosen Wall and Roof <see cref="Material"/> to a given building. /// </summary> /// <param name="building">Building to assign <see cref="Material"/>s to.</param> /// <param name="index"> /// Optional index of Wall and Roof <see cref="Material"/> pair to apply. If this value is not /// set a random <see cref="Material"/> pair will be used. /// </param> internal void AssignNineSlicedMaterials(GameObject building, int?index = null) { // If a specific Material index was given, verify it is a valid index for a Wall and Roof // Material pair. if (index.HasValue) { if (index.Value < 0 || index.Value >= WallMaterials.Length) { Debug.LogError(ExampleErrors.InvalidArrayIndex(this, WallMaterials, "Wall Materials", index.Value)); return; } } else { // Pick a random Material index to use for both Wall and Roof Materials. Not that the same // index will work for both arrays of Materials, as we have already verified that the Wall and // Roof Material arrays are the same length. index = Random.Range(0, WallMaterials.Length); } // Replace building MeshRenderer's sharedMaterials array with chosen Materials. Note that this // must be done by creating a new array of Materials, rather than altering the entries of this // MeshRenderer's sharedMaterials array, as altering the existing array will not actually // change the MeshRenderer's Materials. MeshRenderer buildingMeshRenderer = building.GetComponent <MeshRenderer>(); buildingMeshRenderer.sharedMaterials = new Material[] { WallMaterials[index.Value], RoofMaterials[index.Value] }; }