0

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?

The Crazy
  • 31
  • 7

2 Answers2

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);
KYL3R
  • 3,877
  • 1
  • 12
  • 26
0

Use anchoredPosition to move RectTransform object.

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

Remember that its variable takes Vector2.

KiynL
  • 4,097
  • 2
  • 16
  • 34