Detect every object tagged in an parent.

I’m want to change all mesh renderers material inside this parent on scene, the part to change the material i know but the part to identify all the objects it’s been difficult, and i’m only want to change only the taggeds objects

170983-blueprint.png

this is the code on i’m using to change the textures.

using System.Collections;
using UnityEngine;

public class ThemeScr : MonoBehaviour
{
    public Material Materialc;

    private void Start()
    {
        GameObject[] gameObjects;
        gameObjects = GameObject.FindGameObjectsWithTag("EnemyTexture");
        StartCoroutine(Change());
    }

    IEnumerator Change()
    {
        yield return new WaitForSeconds(5);
        gameObject.GetComponent<MeshRenderer>().material = Materialc;
    }
}

Hello!

I see your problem here, the main issue is that you’re finding an array of tagged objects but not doing anything with that, the game object you’re using in the change coroutine is the game object the script is on. To run this on your detected gameobjects, you should pass them in as an argument.

Secondly, the function you use to find gameobjects will find ALL gameobjects in the scene with that tag, not just the child objects. To find children with a certain tag, we need to do this manually:

List<GameObject> taggedObjects = new List<GameObject>();
foreach(Transform child in transform)
{
    if(child.tag.Equals(“EnemyTexture”))
    {
        taggedObjects.Add(child.gameObject);
    }
}

Thirdly, I am concerned as to why you want to do this…? If it’s changing the material on start then we’ll never see the previous material so not sure why they can’t be the Materialc material all along?

Hope this is helpful!