i got this error please help me

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);
        }
    }
}

As the error tells you, it cant find the method you are trying to call. So inside the type Target, there exists no function TakeDamage which takes a Target as its first argument. The last part is what i find curious, as the thing you pass is ‘damage’, which is a float. But the error suggests otherwise.

In case you experimented around a bit, make sure the code and error you posted come from the same code version. Ie, is this really the code that causes that exact error message?
Also, you might want to post the Target script as well, so we can see the TakeDamage function.

this is the target script i dont know if its wrong or smth

using UnityEngine;

public class Target : MonoBehaviour {
    public float health;

    void Update() {
        if(health <= 0) {
            Destroy(gameObject);
        }
    }

    /// 'Hits' the target for a certain amount of damage
    public void Hit(float damage) {
        health -= damage;
    }
}

You clearly don’t have a method called TakeDamage, so the error is correct. You need a method called TakeDamage so you can call it. I’m going to assume that your Hit method is the one you actually meant to name TakeDamage, or in your other class you should be calling the Hit method.

1 Like

thank you so much im new to coding this was my first long code

1 Like