error CS1061, Pls Help :) (SOLVED)

i dont know what to do, i think im stupid.

Error:

Assets\Gun.cs(24,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?)
using UnityEngine;

public class Gun : MonoBehaviour
{
    public float damage = 10f;
    public float range = 100f;

    public Camera fpsCam;
    // Update is called once per frame
    void Update()
    {
        if(Input.GetButtonDown("Fire1")){
            Shoot();
        }
    }
    void Shoot (){
        RaycastHit hit;
        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);
            }
        }
    }
}

Well does your Target class have a method called TakeDamage or not?

Yes I do

public void TakeDamage(float amount){
        health -=amount;
        if(health <= 0f)
        {
            Die();
        }
        void Die ()
        {
            Destroy(gameObject);
        }

    }
}

Which class is this method in?

Sorry im new to scripting and i dont know what that means, can you explain it?

Where is that TakeDamage method? Which file is it in? Can you show the whole file?

File in unity or on my file explorer

6020486--649286--upload_2020-6-24_20-51-33.png

OR

6020486--649289--upload_2020-6-24_20-52-9.png

Really need help on this, this is a huge part of my game

Fixed nvm

How did you fix it cause I have the same problem

2 Likes

same. if you have fixed it, would you please tell how?

Please don’t respond to old irrelevant posts. It’s against forum rules.

Instead, make your own fresh post. It’s FREE!

When you post, here is how to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

How to understand compiler and other errors and even fix them yourself:

https://forum.unity.com/threads/assets-mouselook-cs-29-62-error-cs1003-syntax-error-expected.1039702/#post-6730855

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379