Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to disable MeshRenderer of child component?

I am new to unity scripting and trying to disable the MeshRenderer component of child object whose name contains collider

enter image description here

using UnityEngine;
using UnityEditor;

// Adds a mesh collider to each game object that contains collider in its name
public class Example: AssetPostprocessor
{
    void OnPostprocessModel(GameObject obj)
    {
        Apply(obj.transform);
    }

    void Apply(Transform obj)
    {
        if (obj.name.ToLower().Contains("collider"))
            obj.gameObject.AddComponent<MeshCollider>();

        foreach (Transform child in obj)
            child.GetComponent<MeshRenderer>().enabled = false;
            Apply(child);
    }
}

Getting this error

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Assets\Editor\CustomImporter.cs(20,13): error CS0103: The name 'child' does not exist in the current context

>Solution :

It looks like you’re missing the curly braces {} for the foreach loop

Try:

    foreach (Transform child in obj)
    {
        child.GetComponent<MeshRenderer>().enabled = false;
        Apply(child);
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading