Transform selectedObject = SCScript.GetCurrentGameObject().transform.Find("Container/ContainerBox/ElementBox");
//Transform selectedObject = SCScript.GetCurrentGameObject().transform; //<--This works fine!!
Debug.Log("Got SelectionBox:"+selectedObject);
if (selectedObject==null) return;
MeshRenderer[] meshes = selectedObject.GetComponentsInChildren<MeshRenderer>();
The Debug.Log shows: GotSelectionBox:UnityEngine.Transform
the GetCurrentGameObject returns a selected GameObject. If I just use the commented line it works, but then I am getting to many Materials, I just want the materials in SelectedGameObject/Container/ContainerBox/ElementBox. ElementBox is an Empty GameObject, which can contain a further GameObject with Materials, that I am trying to get and change. I am checking if there is an ElementBox there, so no errors occur, but it is returning as if it were null even though there is an object there. Is this because it is an empty GameObject?. I get all materials if I just use the currentObject, which is not exactly what I want, but it works. The current GameObject is a loaded prefab and also an empty GameObject, containing multiple other GameObjects.
UPDATE: I tried this and it works ok:
Transform selectedObject = SCScript.GetCurrentGameObject().transform;
Transform[] allChildren = selectedObject.GetComponentsInChildren<Transform>();
foreach (Transform child in allChildren) {
if (child.name=="ElementBox"){
MeshRenderer[] meshes = child.GetComponentsInChildren<MeshRenderer>();
...
}
}