【问题标题】:How can I change a public variable in the Inspector?如何更改 Inspector 中的公共变量?
【发布时间】:2015-03-17 15:37:06
【问题描述】:

我有一个游戏对象。在游戏对象的“检查器”中有一些脚本。现在我想更改附加到脚本的公共变量。我该怎么做?

【问题讨论】:

    标签: c# variables unity3d unityscript unity3d-editor


    【解决方案1】:
    GameObject.Find("Wanderer").GetComponent<SteerForTeether>().TetherPosition = "whatever";
    

    我想应该这样做

    【讨论】:

    • 请注意,如果TestScript 附加到“Wanderer”,则不需要使用GameObject.Find()
    • @Aodai Irshed:不工作。 Gameobject.Find("Wanderer").GetComponent().TetherPosition。找不到 TetherPosition
    • 不,我写对了。这不是问题。当我尝试键入“Tether ...”时,没有显示任何内容。这里:GameObject st = GameObject.GetComponent().XXX;
    • 你应该像我写的那样做 getcomponeny().variable
    • 好吧,肯定不是你在获取组件之前使用 GameObject.find("objectname") 所以它会像这样 GameObject.Find("objectname").GetComponent() .var 或者你只写 GetComponent().var
    【解决方案2】:

    附加到对象的脚本是对象的Component。要访问组件,您需要从游戏对象中调用GetComponent&lt;TYPE&gt;() 方法。组件的类型将与您尝试访问的脚本的名称相同。

    这是一个例子:

    GameObject.Find("Player").GetComponent<PlayerController>().KillPlayer();
    

    在本例中,脚本的名称是PlayerController.csKillPlayer() 是此脚本中定义的公共方法。

    【讨论】:

      猜你喜欢
      • 2019-09-29
      • 2010-09-20
      • 1970-01-01
      • 1970-01-01
      • 2013-08-21
      • 1970-01-01
      • 2015-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多