【问题标题】:How to enable a children of a clone object in Unity?如何在 Unity 中启用克隆对象的子对象?
【发布时间】:2017-05-09 06:07:19
【问题描述】:

我将我的克隆保存在 Unity3D 和 C# 中的 GUI 操作列表中。

button = Instantiate(BlackBox, Vector2.zero, Quaternion.identity) as GameObject;
buttons.Add(button);

如何在this situation 中启用 ArrowRight?

我想联系 ArrowRight 来启用它。我尝试了这种方法的变体:

buttons[index].gameObject.transform.GetChild(1).gameObject.SetActive(true);

但它不起作用。 Objects' components.

【问题讨论】:

  • 看图片,我怀疑你想调用GetChild(0),但这是一个小问题。另一个疑问:BlackBox 对象本身是否被激活?因为如果父母不活跃,您就无法让孩子活跃。
  • 是的,但是这段代码也没有启用 ArrowLeft。
  • 你说得对,我在你回复的时候用另一个问题编辑了我的评论:)
  • 黑盒已激活。
  • 如果这是c#,不要标记它unityscript

标签: c# unity3d clone children


【解决方案1】:

我很快就得到了这个代码来解决你的问题。也许不是最好的解决方案,但如果我正确理解了您的问题,它应该可以工作。

using UnityEngine;

public class blackBox : MonoBehaviour {
    public bool toggleRight;

    // Update is called once per frame
    void Update () {
        if(toggleRight)
        {
            foreach(Transform child in transform)
            {
                if(child.name.Equals("ArrowRight"))
                {
                    child.gameObject.SetActive(!child.gameObject.activeSelf);
                }
            }
            toggleRight = false;
        }
    }
}

这只是一个演示,可以根据您的需要进行修改。

将它放在黑盒游戏对象上,并使用 foreach 激活或停用具有所述名称的游戏对象。

【讨论】:

  • 感谢您的回答,但它不起作用。我以这种方式更改您的代码:foreach(Transform child in buttons[index].transform) { if(child.name.Equals("ArrowRight")) { child.gameObject.SetActive(true); } }
  • 嗯,是的,如果您不想检查游戏对象,孩子们应该怎么做。正如我所说的“撕掉它以满足您的需求”。但它最终做到了你想要的吗?
  • 我的更改也不起作用。我只是在尝试时输入了如何修改您的代码。但是答案找到了:buttons[index].transform.GetChild(1).gameObject.SetActive(true);
【解决方案2】:

已解决:

代替:

buttons[index].gameObject.transform.GetChild(1).gameObject.SetActive(true);

使用:

buttons[index].transform.GetChild(1).gameObject.SetActive(true);

【讨论】:

  • 您确实意识到这两者是完全一样的,对吧?一种是通过一个额外的步骤来获得游戏对象的变换。
  • 我不知道,其中一个不起作用,其中一个起作用。我是 Unity 的初学者,我不知道为什么。
猜你喜欢
  • 2014-11-20
  • 1970-01-01
  • 2018-12-19
  • 1970-01-01
  • 2011-09-19
  • 2018-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多