如何控制数组中的所有游戏对象?

How to control all GameObjects in array?

我有一个生成器,每次按 (1) 都会生成一个球, 每个球都将存储在名为“targetBall”的 Gameobjects 数组中,我有一个 A​​I playerMove() 方法排斥球,但问题是 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 loop 生成的每个球,并给我错误(无法将“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 returns 对象来自集合,因此您无权访问该集合。

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);
                }
            }
        }
    }