【发布时间】:2020-06-18 10:47:27
【问题描述】:
我想制作一个循环遍历所有带有标签的游戏对象的脚本,Enemy
我从here修改了一个代码sn-p,结果出现2个错误。
Cannot Implicitly convert 'Unity.GameObject[]' to 'Unity.GameObject'
和
Error CS1579 foreach statement cannot operate on variables of type 'GameObject' because 'GameObject' does not contain a public instance definition for 'GetEnumerator'
如果有人能告诉我为什么会发生这种情况或解决方案,我将不胜感激,在此先感谢! 这是我的代码:
void FixedUpdate()
{
GameObject objects = GameObject.FindGameObjectsWithTag("Enemy");
var objectCount = objects.Length;
foreach (var obj in objects)
{
// Move the players accordingly
//var rb =
Vector2 direction = (player.position - transform.position).normalized;
obj.rigidbody.velocity = direction * moveSpeed;
}
}
【问题讨论】: