[英]Looping through unity gameobjects
I am trying to make a game where the user controls a square and other enemy shapes move toward it.我正在尝试制作一个游戏,用户控制一个正方形,其他敌人形状朝它移动。 So I thought it would be a good idea to loop through all the gameobjects with the tag Enemy
.所以我认为循环使用标签为Enemy
的所有游戏对象是个好主意。 However I was forced to change a code snippet I found from here , but I then ended up with 2 errors.但是,我被迫更改了从这里找到的代码片段,但最终出现了 2 个错误。
Cannot Implicitly convert 'Unity.GameObject[]' to 'Unity.GameObject'
and和
Error CS1579 foreach statement cannot operate on variables of type 'GameObject' because 'GameObject' does not contain a public instance definition for 'GetEnumerator'
If anyone could tell me why this is happening or a solution to this I would be very grateful, thanks in advance!如果有人能告诉我为什么会发生这种情况或解决方案,我将非常感激,在此先感谢! Here is my code:这是我的代码:
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;
}
}
FindGameObjectsWithTag
as the name hints returns a GameObject[]
. FindGameObjectsWithTag
作为名称提示返回一个GameObject[]
。
In order tog et the attached Rigidbody2D
use GetComponent<Rigidbody2D>
为了获得附加的Rigidbody2D
使用GetComponent<Rigidbody2D>
It should be either GameObject[]
or simply var
它应该是GameObject[]
或简单的var
/*GameObject[]*/ var objects = GameObject.FindGameObjectsWithTag("Enemy");
var objectCount = objects.Length;
foreach (var obj in objects)
{
// Move the players accordingly
var rb = obj.GetComponent<Rigidbody2D>()
Vector2 direction = (player.position - transform.position).normalized;
rb.velocity = direction * moveSpeed;
}
The second one was just a follow up error since you declared objects
as a GameObject
which indeed as the error says has no GetEnumerator
implementation.第二个只是一个后续错误,因为您将objects
声明为GameObject
,实际上正如错误所说,它没有GetEnumerator
实现。
In general it is not the best thing to use FindObjectsWithTag
repeatedly.一般来说,反复使用FindObjectsWithTag
并不是最好的选择。 I would rather use a pattern with a static
list of all existing instances like我宁愿使用带有所有现有实例的static
列表的模式,例如
// Put this component on your enemy prefabs / objects
public class EnemyController : MonoBehaviour
{
// every instance registers to and removes itself from here
private static readonly HashSet<EnemyController> _instances = new HashSet<EnemyController>();
// Readonly property, I would return a new HashSet so nobody on the outside can alter the content
public static HashSet<EnemyController> Instances => new HashSet<EnemyController>(_instances);
// If possible already drag the Rigidbody into this slot via the Inspector!
[SerializedField] private Rigidbody2D rb;
// public read-only access
public Rigidbody2D Rb => rb;
private void Awake()
{
if(!rb) rb = GetComponent<Rigidbody2D>();
_instances.Add(this);
}
private void OnDestroy()
{
_instances.Remove(this);
}
}
and then use然后使用
var enemies = EnemyControler.Instances;
foreach (var enemy in enemies)
{
// Move the players accordingly
Vector2 direction = (player.position - transform.position).normalized;
enemy.Rb.velocity = direction * moveSpeed;
}
Change GameObject
type declaration to var
将GameObject
类型声明更改为var
Or change it to GameObject[]
since FindGameObjectsWithTag
returns an array of GameObject
或者将其更改为 GameObject GameObject[]
因为FindGameObjectsWithTag
返回一个GameObject
数组
Also I wouldnt use FindGameObjectsWithTag
its slow.我也不会使用FindGameObjectsWithTag
它的慢。 More so in a update method在更新方法中更是如此
First off, change GameObject
to GameObject[]
because FindGameObjectsWithTag
returns a GameObject
array.首先,将GameObject
更改为 GameObject GameObject[]
因为FindGameObjectsWithTag
返回一个GameObject
数组。 Be careful not to confuse it with FindGameObjectWithTag
without the s which returns a single GameObject
注意不要将它与没有返回单个FindGameObjectWithTag
的 s 的GameObject
混淆
Secondly, I think it would be best to have a separate Enemy
script or something like that and assign them to each enemy rather than using FindGameObjectsWithTag
which is very slow.其次,我认为最好有一个单独的Enemy
脚本或类似的东西并将它们分配给每个敌人,而不是使用非常慢的FindGameObjectsWithTag
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.