Assets\gunscript.cs(99,24): error CS1061: ‘Target’ does not contain a definition for ‘TakeDamage’ and no accessible extension method ‘TakeDamage’ accepting a first argument of type ‘Target’ could be found (are you missing a using directive or an assembly reference?) <== thats the error.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Gun : MonoBehaviour
{
public float damage = 10f;
public float range = 100f;
public float impactForce = 30f;
public float Firerate = 15f;
public int maxAmmo;
private int currentAmmo;
public float reloadTime = 2f;
private bool isReloading = false;
public Camera fpsCam;
public ParticleSystem muzzleFlash;
public AudioSource shotSound;
public GameObject ImpactEffect;
public Animator ReloadAnimation;
public Text ammoText;
public Text gunName;
private float nextTimeToFire = 0f;
void Start()
{
currentAmmo = maxAmmo;
}
void OnEnable()
{
isReloading = false;
ReloadAnimation.SetBool("Reloading", false);
}
void Update()
{
ammoText.text = currentAmmo + " / " + maxAmmo;
gunName.text = transform.name;
if (isReloading)
return;
if (currentAmmo <= 0)
{
StartCoroutine(Reload());
return;
}
if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 1f / Firerate;
Shoot();
}
if (Input.GetKeyDown(KeyCode.R))
{
StartCoroutine(Reload());
}
}
IEnumerator Reload()
{
Debug.Log("Reloading...");
isReloading = true;
ReloadAnimation.SetBool("Reloading", true);
yield return new WaitForSeconds(reloadTime - .25f);
ReloadAnimation.SetBool("Reloading", false);
yield return new WaitForSeconds(.25f);
currentAmmo = maxAmmo;
isReloading = false;
}
void Shoot()
{
muzzleFlash.Play();
shotSound.Play();
RaycastHit hit;
currentAmmo--;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
// Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent<Target>();
if (target != null)
{
target.TakeDamage(damage);
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * impactForce);
}
GameObject impactGO = Instantiate(ImpactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactGO, 2f);
}
}
}