Combined Cubes with empty GameObject, Apply Material

I created 2 cubes in scene and add an empty GameObject to “group” them together (such that GameObject is the parent of two cubes). I then make it a prefab and try to instantiate with C# scripts.

However, the parent empty GameObject named “T-Joint” doesn’t receive Material & Physic Material. What did I miss to make the material assigned to the children as well?

The codes used:

GameObject g = Instantiate(tJointPrefab, Vector3.zero, Quaternion.identity) as GameObject;
// assign position & scale ( omitted )
int index = Random.Range(0, physMats.Count); // i got couple of different materials, get one from random
g.renderer.material = regularMats[index]; // which is a Material ( basically a diffuse color ) <-- this is Line 56
g.collider.material = physMats[index]; // which is a Physic Material

The error shown is:

MissingComponentException: There is no ‘Renderer’ attached to the “T-Joint(Clone)” game object, but a script is trying to access it.
You probably need to add a Renderer to the game object “T-Joint(Clone)”. Or your script needs to check if the component is attached before using it.
PlatformManager.Start () (at Assets/Scripts/PlatformManager.cs:56)

You have to explicitly set the material for both children. There are a number of ways of approaching the problem. Here is one:

Renderer[] renderers = g.GetComponentsInChildren<Renderer>();
foreach (Renderer rend in renderers) {
    rend.material = regularMats[index];
    rend.collider.material = physMat[index];
}