Question by
Dicken · Jul 12, 2020 at 06:00 PM ·
materialmaterialsraycasting
How do I highlight all the children in a game object instead of just the parent?
Here is my code so far:
[SerializeField] private string selectableTag = "Selectable";
[SerializeField] private Material highlightMaterial;
[SerializeField] private Material Gun_MAT;
private Transform _selection;
// Update is called once per frame
void Update()
{
if (_selection != null)
{
var selectionRenderer = _selection.GetComponent<Renderer>();
selectionRenderer.material = Gun_MAT;
_selection = null;
}
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
var Selection = hit.transform;
if (Selection.CompareTag(selectableTag))
{
var selectionRenderer = Selection.GetComponent<Renderer>();
if (selectionRenderer != null)
{
selectionRenderer.material = highlightMaterial;
}
_selection = Selection;
}
}
}
The code is working as intended, however its only highlighting the parent of the game object. In this case its highlighting the body of a weapon and not highlighting all the attachments.
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613003047im_/https://answers.unity.com/themes/thub/images/avi.jpg)