简体   繁体   English

不知道如何获得敌人的健康

[英]Don't know how to get enemy's health

I have this code and I don't know why hit.collider.gameObject.GetComponent("health") is returning null 我有这段代码,但我不知道为什么hit.collider.gameObject.GetComponent("health")返回null

void Shoot() {
        Vector2 mousePosition = new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, Camera.main.ScreenToWorldPoint (Input.mousePosition).y);
        Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
        RaycastHit2D hit = Physics2D.Raycast (firePointPosition, mousePosition - firePointPosition, bulletRange, whatToHit);
        if (Time.time >= timeToSpawnEffect) {
            Effect ();
            timeToSpawnEffect = Time.time + 1/effectSpawnRate;
        }

        if (hit.collider != null) {
            if (hit.collider.name == "Enemy") {
                Debug.Log(hit.collider.gameObject.GetComponent("health"));
            }
            //Debug.Log("We hit " + hit.collider.name + " and did " + damage + " damage");
        }

    }

Here is my enemy script 这是我的敌人剧本

using UnityEngine;
using System.Collections;

public class EnemyAI : MonoBehaviour
{

    public float health = 100f;
    //... rest of the code
}

You need to get a reference to the script attached to the Enemy. 您需要获得对附加到敌人的脚本的引用。 Then use that script to manipulate the health. 然后使用该脚本操纵运行状况。

Find the GameObject. 找到游戏对象。

 GameObject g = hit.collider.gameObject;

Get the reference to the script. 获取对该脚本的引用。

 EnemyAI e = g.GetComponent<EnemyAI>();

Manipulate the health. 操纵健康。

 e.health = 0f;

In one line if you want to be badass. 如果您想成为坏蛋,请一行。

 hit.collider.gameObject.GetComponent<EnemyAI>().health = 0.0f;

Bonus tip: health should be private and EnemyAI should have a setter and a getter for that variable. EnemyAI提示: health应该是privateEnemyAI应该为该变量设置一个setter和一个getter。

You are using Unity are you not? 您正在使用Unity吗? It looks that way from the code you provided. 从您提供的代码中可以看出。 GetComponent() is a method that returns a reference to a component of the game object. GetComponent()是一种返回对游戏对象组件的引用的方法。 These are the things you drag onto a game object in the Editor. 这些就是您在编辑器中拖动到游戏对象上的东西。 Things like Box-Colliders and Rigidbodys. 像Box-Colliders和Rigidbodys之类的东西。 The code you wrote will not return anything because there is no game component in Unity called "health". 您编写的代码不会返回任何内容,因为Unity中没有称为“健康”的游戏组件。 To get the health of the enemy,you need to set up a reference to the script controlling the enemy and get its health value from there. 为了获得敌人的生命值,您需要建立对控制敌人的脚本的引用,并从那里获取其生命值。

However you would have to get the reference of the script that is attached to the GameObject. 但是,您将必须获取GameObject附带的脚本的引用。 To do this you would need to use the following code. 为此,您需要使用以下代码。

GameObject target;

Then in your shoot method you update the reference to the target. 然后,在您的射击方法中,将参考更新为目标。

if(hit.collider.gameObject != target) 
{
    target = hit.collider.gameObject.GetComponent<EnemyAI>();
}

The reaason I put an if() statement around it is so that you're not overloading the CPU with GetComponent requests if the target hasn't already changed. 我在其周围放置了一个if()语句,以便在目标尚未更改的情况下,不会因GetComponent请求而使CPU过载。

From here you would simply change the value by using things such as 在这里,您只需使用诸如

target.value = newValue;
target.SomeFunction();

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

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