【问题标题】:I want to Change The sprite through code (I tried this.GameObject<SpriteRenderer>().sprite = spriteName)我想通过代码更改精灵(我试过 this.GameObject<SpriteRenderer>().sprite = spriteName)
【发布时间】:2020-07-03 16:56:27
【问题描述】:

我试过this.GameObject&lt;SpriteRenderer&gt;().sprite = spriteName;,但它似乎不起作用它只是给我错误:

找不到类型或命名空间名称“sprite”(您是 缺少 using 指令或程序集引用?)

public void TheBlueCircle()
{
    this.GameObject.GetComponent<SpriteRenderer>().sprite = BlueCircle;
}

【问题讨论】:

  • 这需要更多上下文才能理解问题。
  • 我想使用一个按钮更改精灵,我创建了一个变量(public Sprite blueCircle;)并引用了它,但它仍然显示错误
  • 但您的错误不在 BlueCircle/blueCircle 上。对于命名空间错误,上下文至少意味着您列出了 using 指令。
  • 请添加完整代码

标签: c# unity3d syntax-error


【解决方案1】:

this.GameObject不正确。您需要使用 GameObject 的实例而不是类,因此它将是 this.gameObject.GetComponent&lt;&gt;()

在很多情况下,this.gameObject 不是必需的,所以你可能只写GetComponent&lt;&gt;()

【讨论】:

  • (我当然怀疑这里的情况,但是)如果GameObject 是 OP 类的一个字段呢?像例如public GameObject GameObject; ;)
  • 是的,您可以使用与类同名的变量,但绝对不推荐...
猜你喜欢
  • 1970-01-01
  • 2018-05-18
  • 2021-04-09
  • 2013-01-12
  • 2021-12-30
  • 1970-01-01
  • 1970-01-01
  • 2015-12-13
  • 1970-01-01
相关资源
最近更新 更多