如果我将对象分配为GameObject,则无法使RectTransform工作。如何解决?

发布于 2025-01-29 11:11:24 字数 262 浏览 4 评论 0原文

我想通过这条代码来提供我的父gamobject 库存 spicific rec坐标: intectory.getComponent< recttransform>()

。继续将rec坐标更改为世界配音。我已经尝试将库存分配为A transform recttransform ,但它说该类型中有一个失误。

如何通过修复我的代码或将其分配为不同的东西来解决该问题?

I want to give my Parent GameObject Inventory spicific rec-coordinates via this line of code:
Inventory.GetComponent<RectTransform>().position = new Vector3(500f, 0f, 0f);

In the editor, the object Inventory is assigned as a GameObject, so it keeps changing the rec-coordinates to world-coordinates. I've tried assigning the Inventory as a Transform and RectTransform but it says that there is a missmatch in the type.

How can I fix that, either by fixing my code or by assigning it as something different?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七七 2025-02-05 11:11:25

使用锚定移动recttransform对象。

GetComponent<RectTransform>().anchoredPosition = new Vector2(500f, 0);

请记住,其变量采用vector2

Use anchoredPosition to move RectTransform object.

GetComponent<RectTransform>().anchoredPosition = new Vector2(500f, 0);

Remember that its variable takes Vector2.

抱着落日 2025-02-05 11:11:24

您应该修改RectTransform的局部问题:

Inventory.GetComponent<RectTransform>().localPosition = new Vector3(500f, 0f, 0f);

即使没有 recttransform(localpositions似乎是在变换&amp; recttransform之间)的(localpositions):

Inventory.transform.localPosition = new Vector3(500f, 0);

You should modify the RectTransform's localPosition:

Inventory.GetComponent<RectTransform>().localPosition = new Vector3(500f, 0f, 0f);

Even without RectTransform (localPositions seems to be inherited/shared between Transform&RectTransform):

Inventory.transform.localPosition = new Vector3(500f, 0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文