如何控制数组中的所有GameObject?

我有一个生成器,每次我按下(1)键时就会生成一个球,每个球都会被存储在一个名为”targetBall“的Gameobjects数组中。我有一个AI玩家,它使用Move()方法来排斥球,但问题是AI玩家只能看到数组中的第一个球,即代码中显示的球[0],我该如何让AI玩家看到所有生成的球(无限个球)?我尝试使用for循环但没有成功(注意:其他一切都运行得很完美)

void Move()    {        targetBall = GameObject.FindGameObjectsWithTag("HokeyBall");            if (targetBall[0].GetComponent<HokyBall>().ballDirection == Vector3.right)            {                ballPos = targetBall[0].transform.localPosition;                if (transform.localPosition.x < Lboundry && ballPos.x > transform.localPosition.x)                {                    transform.localPosition += new Vector3(speed * Time.deltaTime, 0, 0);                }                if (transform.localPosition.x < Lboundry && ballPos.x < transform.localPosition.x)                {                    transform.localPosition += new Vector3(-speed * Time.deltaTime, 0, 0);                }            }}

这是我尝试使用for循环查找每个生成的球的方法,但出现了错误(无法将“int”转换为“GameObject”)

void Move()        {            targetBall = GameObject.FindGameObjectsWithTag("HokeyBall");foreach (GameObject i in targetball){                    if (targetBall[i].GetComponent<HokyBall>().ballDirection == Vector3.right)                {                    ballPos = targetBall[i].transform.localPosition;                        if (transform.localPosition.x < Lboundry && ballPos.x > transform.localPosition.x)                    {                        transform.localPosition += new Vector3(speed * Time.deltaTime, 0, 0);                    }                    if (transform.localPosition.x < Lboundry && ballPos.x < transform.localPosition.x)                    {                        transform.localPosition += new Vector3(-speed * Time.deltaTime, 0, 0);}                    }                }    }

回答:

foreach返回集合中的对象,因此你无法访问该集合。

使用foreach的方法 –

    void Move()    {        targetBall = GameObject.FindGameObjectsWithTag("HokeyBall");        foreach (GameObject go in targetball)        {            if (go.GetComponent<HokyBall>().ballDirection == Vector3.right)            {                ballPos = go.transform.localPosition;                if (transform.localPosition.x < Lboundry && ballPos.x > transform.localPosition.x)                {                    transform.localPosition += new Vector3(speed * Time.deltaTime, 0, 0);                }                if (transform.localPosition.x < Lboundry && ballPos.x < transform.localPosition.x)                {                    transform.localPosition += new Vector3(-speed * Time.deltaTime, 0, 0);                }            }        }    }

使用for循环的方法 –

    void Move()    {        targetBall = GameObject.FindGameObjectsWithTag("HokeyBall");        for (int i = 0; i < targetBall.Length; i++)        {            if (targetBall[i].GetComponent<HokyBall>().ballDirection == Vector3.right)            {                ballPos = targetBall[i].transform.localPosition;                if (transform.localPosition.x < Lboundry && ballPos.x > transform.localPosition.x)                {                    transform.localPosition += new Vector3(speed * Time.deltaTime, 0, 0);                }                if (transform.localPosition.x < Lboundry && ballPos.x < transform.localPosition.x)                {                    transform.localPosition += new Vector3(-speed * Time.deltaTime, 0, 0);                }            }        }    }

Related Posts

Keras Dense层输入未被展平

这是我的测试代码: from keras import…

无法将分类变量输入随机森林

我有10个分类变量和3个数值变量。我在分割后直接将它们…

如何在Keras中对每个输出应用Sigmoid函数?

这是我代码的一部分。 model = Sequenti…

如何选择类概率的最佳阈值?

我的神经网络输出是一个用于多标签分类的预测类概率表: …

在Keras中使用深度学习得到不同的结果

我按照一个教程使用Keras中的深度神经网络进行文本分…

‘MatMul’操作的输入’b’类型为float32,与参数’a’的类型float64不匹配

我写了一个简单的TensorFlow代码,但不断遇到T…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注