I have this code and I don't know why hit.collider.gameObject.GetComponent("health")
is returning 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
}