0

我有坐标 x 和 y 的数组

x = new int[18];
y = new int[15];
x[0] = -404;
y[0] = -226;
for (int i = 1; i < 18; i++)
    x[i] = x[i - 1] + 30;
for (int i = 1; i < 15; i++)
    y[i] = y[i - 1] + 30;

我从数组中设置随机坐标。但是当我启动程序时它们是不正确的。大多数数字都超出了数组。无法理解为什么。可能是我设置的位置不正确吗?

int xCor = x[(int)Random.Range(0, x.Length - 1)];
int yCor = y[(int)Random.Range(0, y.Length - 1)];
transform.position = new Vector2(xCor, yCor);

我需要设置新坐标。例如 x = 24,y = 50。

苹果不在绿区: 在此处输入图像描述

4

1 回答 1

2

Use RectTransform.anchoredPosition property instead of transform.position, like this

RectTransform rectTransform = GetComponent<RectTransform>();
rectTransform.anchoredPosition = new Vector2(xCor, yCor);

You are using a canvas GameObject which doesn't have a normal transform component.

于 2018-07-17T22:24:23.580 回答