在我的脚本中激活时,GameObject 预制件未显示在场景中

GameObject prefab is not showing in scene when activated in my script

我有 2 个 classes 附加到 2 个不同的 GUI,PlayerInfoGUI 和 DiplomacyGUI。

我正在尝试让 PlayerInfoGUI 中的方法动态创建一个按钮列表,单击该按钮时将拉出 DiplomacyGUI。 (GUI 和动态按钮都有自己创建的预制件)

My PlayerInfoGUI class 使用 PopulatePlayerList 方法动态填充带有按钮的面板。

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
using System.Collections.Generic;


public class PlayerInfoGUI : MonoBehaviour
{
    public Image[] guiElements;
    public GameObject playerInfoButtonPrefab, canvasParent;
    public GameObject diplomacyMenu;

    void Awake ()
    {
        PopulatePlayerList (GameEngine.competingPlayers);
    }

    void PopulatePlayerList (List<CompetingPlayer> players)
    {

        for (int i = 0; i < players.Count; i++) {
            GameObject go = (GameObject)Instantiate 
            (playerInfoButtonPrefab);
            Button playerInfoButton = go.GetComponent<UnityEngine.UI.Button> 
                                                                         ();
            CompetingPlayer receivingPlayer = players [i];
            playerInfoButton.onClick.AddListener (() => handleDiplomacyMenu 
                                                         (receivingPlayer));
            go.transform.SetParent (canvasParent.transform, false);
        }
    }

    public void handleDiplomacyMenu (CompetingPlayer receivingPlayer)
    {
        diplomacyMenu.SetActive (true);
    }
}

PlayerInfo 按钮上的侦听器在单击时触发,但外交菜单游戏对象未出现在场景中。我读过的大多数研究都说这应该是一个简单的 diplomacyMenu.SetActive(true),或者 diplomacy.gameObject.SetActive(true),但这不起作用。

我已经确认代码正在运行,但是看不到对象。 提前致谢!

PlayerInfoPrefab

PlayerInfoButtonPrefab

DiplomacyPrefab

当 diplomacyMenu 已经是一个游戏对象时,您正在使用 diplomacyMenu.gameObject,这可能会导致问题。还要确保由 diplomacyMenu 表示的预制件已启用其子项。我认为 SetActive() 只是将顶级父级设置为启用,而不是任何嵌套对象。