团结,对敌,多次BUG

Unity, on enemy, multiple times BUG

我有一个敌人的预制件,如果我将同一个预制件放在同一个场景中超过 1 次,只有其中 1 个起作用,那个起作用的总是我放在场景中的最后一个预制件:

还有其他敌人,但部分脚本似乎不起作用,而另一部分是。

Foc scriptFoc;
    GameObject focGO;
    private Animator animator;
    private Transform target;
    private Vector3 relativePoint;

    private float tempsActual = 0.0f;
    private bool foc = true;

    public float duracioNormal;
    public float duracioFoc;

    void Start () {
        target = GameObject.FindGameObjectWithTag("Player").transform;
        animator = this.GetComponent<Animator>();
        focGO = GameObject.FindGameObjectWithTag("Foc");
        scriptFoc = focGO.GetComponent<Foc>();
        scriptFoc.actiu();
    }

    void Update ()
    {
        if (drac_Actiu())
        {
            if(!foc)
            {
                tempsActual += Time.deltaTime;
                if (tempsActual >= duracioNormal)
                {
                    scriptFoc.actiu();
                    tempsActual = 0.0f;
                    foc = true;
                    animator.SetBool("Foc", true);
                }
            }
            else if(foc)
            {
                tempsActual += Time.deltaTime;
                if (tempsActual >= duracioFoc)
                {
                    scriptFoc.inactiu();
                    tempsActual = 0.0f;
                    foc = false;
                    animator.SetBool("Foc", false);
                }
            }
        }
    }

    private bool drac_Actiu()
    {
        relativePoint = transform.InverseTransformPoint(target.position); //Punt del jugador

        if (relativePoint.x < 2.0f || relativePoint.x > -2.0f)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

你有什么想法吗? 谢谢!

没有足够的信息可以确定。脚本的哪一部分不起作用?你说它的一部分呢?

您是否忘记在检查器中为某些游戏对象设置 public 变量的值?

是否每个游戏对象都标记有 "Foc" 如果是,focGO 可能是您添加的最后一个游戏对象,因为它只是寻找一个然后停止检查。

这些是我根据所提供的信息做出的猜测。

focGO = GameObject.FindGameObjectWithTag("Foc");

除非我读错了,否则你让你的每个实例都通过其标签查找游戏对象,因此它们完全有可能找到相同的对象(你添加到的最后一个对象)现场)。

我相信您可能只想选择 "this.[whatever]"。例如:

scriptFoc = this.GetComponent<Foc>();