【发布时间】:2020-04-17 00:18:39
【问题描述】:
我试图弄清楚如何使一个对象移动到另一个具有特定标签的对象的位置。我目前有一个对象数组,其中包含对象拥有的所有“选择”。然后我得到一个从 0 到找到的对象数的随机数。我测试了这部分,它似乎运行良好。
我坚持的是将脚本所在的对象移动到脚本找到的对象的位置。我认为我需要做的是获取与索引号及其位置关联的对象,但这不起作用。提前致谢!抱歉,如果这个问题的结构不正确,这是我的第一篇文章!
public class objective : MonoBehaviour
{
GameObject moveTo;
public GameObject[] gameObjectChoices;
public float choiceNumber;
void Start()
{
//get objects with tag and apply it to object array
gameObjectChoices = GameObject.FindGameObjectsWithTag("RandomCheesePoint");
//choose one of the gameObjects in array
choiceNumber = Random.Range(0, gameObjectChoices.Length);
Debug.Log(choiceNumber);
//move object to random object found
transform.position = gameObjectChoices[choiceNumber].gameObject.transform.position;
//problem occurs at gameObjectChoices[choiceNumber]
//error message says "Cannot implicitly convert type 'float' to 'int' An explicit conversion exists (are you missing a cast?)"
}
}
【问题讨论】:
标签: c# visual-studio unity3d