预制件的实例没有被单独控制

Instances of prefab aren't being controlled separately

我在一个关卡中设置了多个敌人,它们都使用相同的行为和动画脚本。当我击中或杀死其中一个时,他们所有人都会被击中或杀死。我需要它们作为单独的实例运行。

我试过只引用脚本的一个实例:

 private GoblinBehaviour goblin;
  goblin = GetComponent<GoblinBehaviour>();
        goblin.IncrementHits(1);

但这不起作用。出现一个错误,指出无法使用实例访问脚本,而是需要一个类型。

命中检测脚本代码:

   public class RangedDetection : MonoBehaviour
   {
   private GoblinBehaviour goblin;
   void OnTriggerEnter(Collider other)
    {
    //on colliding destroy rocks after its life time
    Destroy(gameObject, rockLifeTime);

    if (other.gameObject.tag.Equals("Enemy"))
    //if (other.tag == "Enemy")
    {
        Destroy(other.gameObject);
    }
    else if (other.gameObject.tag.Equals("Goblin"))//goblin should play 
 death animation
    {
       goblin = GetComponent<GoblinBehaviour>();
        goblin.IncrementHits(1);
        GetComponent<BoxCollider>().enabled = false; //Removing hit 
 collider so it only hits target once.
    }
 }
 }

goblin 脚本的简化代码:

 public class GoblinBehaviour : MonoBehaviour
 {
Transform player;

public static bool isDead;
public static bool isPunching;
public static bool isHit;
public GameObject particles;
public Animator anim;


 public void IncrementHits(int hitCount)
 {
    isHit = true;
    hits += hitCount;
    if (hits >= 2) isDead = true;


}

void Die()
{
    Instantiate(particles, transform.position, transform.rotation);
    Destroy(gameObject);

}


void updateAnim()
{
    anim.SetBool("Punch_b", isPunching);
    anim.SetBool("hit", isHit);

}

}

事物应该动画并单独行动,我不确定如何只引用脚本的当前实例。

虽然你的代码不完整,问题也说不准,但看起来你使用statics不当。

静态属性与实例类似。换句话说,您的所有地精实例都共享任何静态属性(isPunching、isHit、isDead 等)。将这些值设置为静态允许您直接引用它们而无需获取您正在影响的实例,但会导致您立即更新所有地精。

您的解决方案将涉及从您的 GoblinBehaviour 属性中删除静态修饰符,除非这些属性是要在所有实例之间共享的。