简体   繁体   English

尝试检测碰撞游戏时出现 Unity 错误 Object 应用损坏 (Unity3d - C#)

[英]Unity Error When Trying To Detect Collided Game Object To Apply Damage (Unity3d - C#)

I am attempting to detect the tag of the enemy collided with, and if its tag is Enemy, then access it's current health within its script (EnemyAI) and deduct the attack damage amount.我正在尝试检测碰撞的敌人的标签,如果它的标签是敌人,则在其脚本(EnemyAI)中访问它的当前健康并扣除攻击伤害量。

The code seems correct however i'm getting this error in Unity;代码似乎是正确的,但是我在 Unity 中遇到了这个错误; "The name 'collision' does not exist in the current context". “当前上下文中不存在名称‘碰撞’”。

I have multiple enemies and i've tagged all with Enemy.我有多个敌人,我已经用敌人标记了所有敌人。 The idea being that if the player attacks one of them, it will detect that it's an enemy due to its tag and then access its script to apply the damage.这个想法是,如果玩家攻击其中一个,它会根据它的标签检测到它是一个敌人,然后访问它的脚本来施加伤害。

The code I currently have is:我目前拥有的代码是:

                //Detect enemies in range of attack
                Collider[] hitEnemies = Physics.OverlapSphere(attackPoint.position, attackRange, enemyLayers);

                //Hurt enemy
                if (collision.gameObject.tag == "Enemy")
                {
                    //Damage them
                    collision.gameObject.GetComponent<EnemyAI>().currentHealth -= attackDamage;
                }

I also tried using public void OnCollisionEnter(Collision collision) , adding the code to it and then calling it but I need to pass a value into it.我还尝试使用public void OnCollisionEnter(Collision collision) ,将代码添加到它,然后调用它,但我需要将一个值传递给它。 I tried passing in Collider and I get an error saying its a type.我尝试传入 Collider,但我收到一个错误,说它是一种类型。 I tried collider instead and I get the same error "The name 'collision' does not exist in the current context".我尝试了对撞机,但我得到了同样的错误“当前上下文中不存在名称'碰撞'”。

I'm stuck, i'd appreciate the help a lot.我被困住了,非常感谢您的帮助。

I've reverted back to calling OnCollisionEnter(collision);我已经恢复调用OnCollisionEnter(collision); and then it goes to:然后转到:

        public void OnCollisionEnter(Collision collision)
        {
            if (collision.gameObject.tag == "Enemy")
            {
                //Damage them
                collision.gameObject.GetComponent<EnemyAI>().currentHealth -= attackDamage;
            }
        }

Same error message: The name 'collision' does not exist in the current context".相同的错误消息:名称 'collision' 在当前上下文中不存在”。

You've mixed OnCollision syntax into your code.您已将 OnCollision 语法混合到代码中。 Currently there is no collision created/set anywhere in your code (context).目前,您的代码(上下文)中的任何地方都没有创建/设置冲突 There are only colliders that are returned by the overlapsphere.只有重叠球返回的对撞机 Hence why you get the error.因此,为什么您会收到错误消息。 You need to handle them a bit differently.您需要以不同的方式处理它们。

//Detect enemies in range of attack
Collider[] hitEnemies = Physics.OverlapSphere(attackPoint.position, attackRange, enemyLayers);

foreach(Collider enemyCol in hitEnemies){
  //Hurt enemy
  if (enemyCol.gameObject.tag == "Enemy")
  {
    //Damage them
    enemyCol.gameObject.GetComponent<EnemyAI>().currentHealth -= attackDamage;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM