英文:
How to disable MeshRenderer of child component?
问题
我是新手的unity脚本编写,试图禁用包含collider
的子对象的MeshRenderer
组件。
using UnityEngine;
using UnityEditor;
// 为每个包含“collider”名称的游戏对象添加一个网格碰撞器
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);
}
}
出现以下错误:
Assets\Editor\CustomImporter.cs(20,13): error CS0103: The name 'child' does not exist in the current context
英文:
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
答案1
得分: 0
看起来你忘记了foreach
循环的花括号{}。
尝试:
foreach (Transform child in obj)
{
child.GetComponent<MeshRenderer>().enabled = false;
Apply(child);
}
英文:
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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论