【问题标题】:How to get the variable name of GameObject如何获取GameObject的变量名
【发布时间】:2019-05-27 21:19:50
【问题描述】:

我有一个 GameObject 列表。我正在做的是每当我实例化存储在 GameObject 变量中的预制件时,然后再将其销毁。

List<GameObject> listofGameObject;
blueLine = Instantiate(monitorShapes, transform.position, transform.rotation);

稍后我将使用 listofGameObject[2] 访问该游戏对象。我想要实现的是获取存储在 listofGameObject 的索引 2 中的 GameObject 的变量名称,即 blueLine。我尝试使用 listofGameObject[2].name 但它返回资产中预制件的名称。

【问题讨论】:

    标签: c# unity3d game-development


    【解决方案1】:

    实例化后,将GameObject的名称设置为变量名

    blueLine = Instantiate(monitorShapes, transform.position, transform.rotation);
    blueLine.name = nameof(blueLine);
    

    【讨论】:

      【解决方案2】:

      预制件以预制资产的名称生成。

      有没有包含变量名 blueLine 的脚本?如果是,那么您可能希望在引用变量名之前获取该组件。

      示例。

      List<GameObject> listofGameObject;
      blueLine = Instantiate(monitorShapes, transform.position, transform.rotation);
      
      -- listofGameObject[2].GetComponent<[ScriptName]>().variableName;
      

      如果有帮助请告诉我

      【讨论】:

        【解决方案3】:

        为什么需要获取变量名?您可以从列表中删除游戏对象,将其存储在游戏对象中,然后销毁该游戏对象。

        所以代码看起来像这样:

        //Creates the list
        List<GameObject> listOfGameObjects = new List<GameObject>();
        //Creates the blueLine GameObject
        blueLine = Instantiate(monitorShapes, transform.position, transform.rotation);
        //Adds the blueLine object to the list
        listOfGameObjects.Add(blueLine);
        

        然后,当您想从列表中删除对象时,请执行以下操作:

        //Removes the blueLine object from the list
        listOfGameObjects.Remove(blueLine);
        //Destroys the blueLine Object from the scene
        Destroy(blueLine);
        

        我希望这是您正在寻找的解决方案

        【讨论】:

          猜你喜欢
          • 2014-06-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-30
          • 2022-01-17
          • 1970-01-01
          • 2012-11-13
          相关资源
          最近更新 更多