C# Unity UI似乎正在显示旧的(缓存的)值

C# Unity UI似乎正在显示旧的(缓存的)值,c#,user-interface,unity3d,C#,User Interface,Unity3d,我有一个UIPanel预置(ItemDetailedView游戏对象),它有自己的脚本(ItemDetailedView.cs) 我实例化ItemDetailedView游戏对象,然后获取其组件脚本,并告诉它显示项目的详细信息 问题是,“详细信息”视图总是显示我查看的最后一项。 即使游戏停止并重新启动,第一个项目视图也将是我上次运行游戏时查看的最后一个项目 我是否应该打一个“强制GUI更新”电话 //This is set to the prefab in the inspector publi

我有一个UIPanel预置(ItemDetailedView游戏对象),它有自己的脚本(ItemDetailedView.cs)

我实例化ItemDetailedView游戏对象,然后获取其组件脚本,并告诉它显示项目的详细信息

问题是,“详细信息”视图总是显示我查看的最后一项。 即使游戏停止并重新启动,第一个项目视图也将是我上次运行游戏时查看的最后一个项目

我是否应该打一个“强制GUI更新”电话

//This is set to the prefab in the inspector
public GameObject ViewItemPrefab

//called from an inventory view button listener
private void ViewDetails(Item item)
{
     //Shows the correct item.Name
     print("about to show details for " + item.Name);

     //instantiates nicely
     Instantiate(ViewItemPrefab, transform.parent);
     ViewItemPrefab.GetComponent<GUIDetailedItemViewWindow>().Show(item);
}

public class GUIDetailedItemViewWindow : MonoBehaviour
{
    private Item item;

    //text UI object set in the inspector
    public Text ItemNameText;

    public void Show(Item itemToShow)
    {
        //this prints the right Name
        print("itemToShow name: " + itemToShow.Name);

        item = itemToShow;

        //this prints the right Name too!
        print("Item to view details for: " + item.Name);

        //this shows the Name of the last Item we were looking at!
        // this is the problem here!
        // it will even show the name of the item I looked at last time I ran the game!
        ItemNameText.text = item.Name;


        //now it gets really weird
        //this shows the correct item name!
        print("ItemNameText.text is " + ItemNameText.text);

    }
}

//这是在inspector中设置为预置的
公共游戏对象ViewItemPrefab
//从清单视图按钮侦听器调用
私有无效视图详细信息(项目)
{
//显示正确的项目名称
打印(“即将显示“+项目名称”的详细信息);
//很好地实例化
实例化(ViewItemPrefact,transform.parent);
viewItemPrefabrize.GetComponent().Show(项);
}
公共类指南TailedItemView指南:单一行为
{
私人物品;
//在检查器中设置的文本UI对象
公共文本项目名称文本;
公共作废显示(项目项显示)
{
//这是正确的名字
打印(“itemToShow名称:+itemToShow.name”);
item=itemToShow;
//这个名字也印对了!
打印(“要查看详细信息的项目:“+项目.名称”);
//这显示了我们正在查看的最后一个项目的名称!
//这就是问题所在!
//它甚至会显示我上次运行游戏时查看的项目的名称!
ItemNameText.text=item.Name;
//现在真的很奇怪了
//这将显示正确的项目名称!
打印(“ItemNameText.text为”+ItemNameText.text);
}
}
下次我尝试查看某个项目的详细信息时,它将显示我们尝试查看其详细信息的最后一个项目

我完全糊涂了

谢谢

线路

ViewItemPrefab.GetComponent<GUIDetailedItemViewWindow>().Show(item);
viewItemPrefabride.GetComponent().Show(项目);
获取预置的组件,而不是新实例化的对象

这就是为什么下次创建窗口时会看到这些值,也是为什么它会在游戏重置期间持续存在的原因(只有场景被恢复,但您修改了预设)。要访问新对象,请执行以下操作

GameObject details = Instantiate(ViewItemPrefab, transform.parent);
details.GetComponent<GUIDetailedItemViewWindow>().Show(item);
GameObject details=实例化(ViewItemPrefab,transform.parent);
details.GetComponent().Show(项);
线路

ViewItemPrefab.GetComponent<GUIDetailedItemViewWindow>().Show(item);
viewItemPrefabride.GetComponent().Show(项目);
获取预置的组件,而不是新实例化的对象

这就是为什么下次创建窗口时会看到这些值,也是为什么它会在游戏重置期间持续存在的原因(只有场景被恢复,但您修改了预设)。要访问新对象,请执行以下操作

GameObject details = Instantiate(ViewItemPrefab, transform.parent);
details.GetComponent<GUIDetailedItemViewWindow>().Show(item);
GameObject details=实例化(ViewItemPrefab,transform.parent);
details.GetComponent().Show(项);

是!!固定的。谢谢对固定的。谢谢