I am new to unity scripting and trying to disable the MeshRenderer component of child object whose name contains collider
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
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);
}
