我有這樣的代碼,我不知道爲什麼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");
}
}
這裏是我的敵人腳本
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour
{
public float health = 100f;
//... rest of the code
}
值是你的類名爲'health',比如'health.cs'? 'GetComponent'區分大小寫。請參閱http://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html – 2014-10-20 01:31:23
上的最後描述,該類名爲EnemyAI.cs, 健康是一個公共浮動 – 2014-10-20 01:35:24