我有一个在Unity中放置墙壁的WallCreator脚本,还有另一个WallCreatorSwitcher脚本,通过检查开关来打开/关闭WallCreator。 我还想使用GetComponent<WallCreator>().wallPrefab.layer = LayerMask.NameToLayer("LayerName");
在那里更改wallPrefab的图层,因此当选中开关时(WallCreator ON),图层为"Ignore Raycast" - 它可以正常工作,如果取消选中开关(WallCreator OFF),则图层为"Default" - 但问题是,在这种情况下它没有更改图层。
public class WallCreatorSwitcher : MonoBehaviour {
public Toggle toggle;
public Text text;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (toggle.isOn)
{
GetComponent<WallCreator>().enabled = true;
Debug.Log("Wall Creator is ON");
text.enabled = true;
GetComponent<WallCreator>().wallPrefab.layer = LayerMask.NameToLayer("Ignore Raycast");
}
else
{
GetComponent<WallCreator>().enabled = false;
Debug.Log("Wall Creator is OFF");
text.enabled = false;
GetComponent<WallCreator>().wallPrefab.layer = LayerMask.NameToLayer("Default");
}
}
}
GetComponent<WallCreator>().enabled = false;
使该组件完全无法交互,因此你可能需要先将其打开、更改图层,然后再关闭它。或者,在关闭之前切换图层。 - Alfie GoodacreGetComponent<WallCreator>().enabled = false;
(这样我就可以在切换未选中时放置墙壁),该层仅针对新对象更改。我的意思是,例如,我使用切换器 ON 放置 wall1 - 它的层是 "Ignore Raycast",然后我将切换器切换为 OFF,放置 wall2 - 它的层是 "Default",但 wall1 的层不会更改。 - artur47wienGameObject [] walls = GameObject.FindGameObjectsWithTag(“wall”);
然后您可以通过foreach循环对它们进行操作。 - Alfie GoodacreWallCreator
脚本,并描述每个脚本所附加的位置。 - Programmer