Im pretty new to unity and im making an FPS game, im current making the shooting script but i cant figure one thing out. I want to make the shooting with Raycasts (they are better that instanting bullets) but i dont know how to apply damage. Here is what i tried:
The gun script:
using UnityEngine;
using System.Collections;
public class GunShooting : MonoBehaviour {
Transform Effect;
public float TheDamage = 100;
object particleClone;
RaycastHit hit;
void Update (){
Vector3 fwd = transform.TransformDirection(Vector3.forward);
if (Input.GetMouseButtonDown(1))
{
if (Physics.Raycast(transform.position, fwd, 1000))
{
hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
}
}
}
}
The zombie AI:
using UnityEngine;
using System.Collections;
public class zombieAI : MonoBehaviour {
public float startingHealth = 100;
public float health;
// Use this for initialization
void Start () {
health = startingHealth;
}
// Update is called once per frame
void Update () {
if (health <= 1)
{
DestroyObject (gameObject);
}
}
public void ApplyDamage (float TheDamage)
{
health -= TheDamage;
}
}
When i shoot it doesnt deal damage and i get one error:
NullReferenceException: Object reference not set to an instance of an object
GunShooting.Update () (at Assets/Scripts/GunShooting.cs:18)