Why won't my bullet script work?

Continuing the discussion from How do I make it so that my bullet projectile for my gun does damage?:

When I tried the bullet script it gave me an error saying Component does not contain a definition for TakeDamage and no accessible extension method TakeDamage accepting a first argument of type ‘Component’ could be found. What do I do?(New to Unity)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour
{
    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Target"))
        {
            float dmg = 10;
            other.GetComponent("Target").TakeDamage(dmg);
            
            Destroy(gameObject);
        }
        else
        {
            Destroy(gameObject);
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour
{
    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Target"))
        {
            float dmg = 10;

            // Make sure the "Target" script is attached to the "other" GameObject
            Target targetScript = other.GetComponent<Target>();

            if (targetScript != null)
            {
                // Call the TakeDamage method on the Target script
                targetScript.TakeDamage(dmg);
            }

            Destroy(gameObject);
        }
        else
        {
            Destroy(gameObject);
        }
    }
}

Make sure to replace "Target" with the actual tag name you want to check and update Target with the correct script name that contains the TakeDamage method.

It didn’t do damage. Here is the Target script. It may be the problem.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Target : MonoBehaviour
{
    private float health = 10;
    public void TakeDamage(float dmg)
    {
        health = health - dmg;
    }
}

it looks correct maybe the issue is in the gameobject itself, make sure the target has the Component Target attached to it. Also make sure bullet has collider on it and its trigger, take into account that the target also needs to have a collider and a rigidbody involved in the collision.

Note: Both GameObjects must contain a Collider component. One must have Collider.isTrigger enabled, and contain a Rigidbody. If both GameObjects have Collider.isTrigger enabled, no collision happens. The same applies when both GameObjects do not have a Rigidbody component.

I did everything and It still doesn’t do damage I can’t figure it out they both have colliders(If a mesh collider counts)and they both have rigid body could it be the character model it self?

Lets make it simple!
Please add Debug.Logs to your Bullet script to help diagnose the problem:

public class Bullet : MonoBehaviour
{
    private void OnTriggerEnter(Collider other)
    {
        Debug.Log("Bullet collided with: " + other.gameObject.name); // Debug log to check if OnTriggerEnter is called
  
        if (other.CompareTag("Target"))
        {
            float dmg = 10;
  
            // Make sure the "Target" script is attached to the "other" GameObject
            Target targetScript = other.GetComponent<Target>();
  
            if (targetScript != null)
            {
                targetScript.TakeDamage(dmg);
                Debug.Log("Target's health after damage: " + targetScript.health); // Debug log to check if TakeDamage is executed
            }
  
            Destroy(gameObject);
        }
        else
        {
            Destroy(gameObject);
        }
    }
}

It gives me the error ‘Target.health’ is inaccessible due to a protective layer.

It also gives me “You are trying to save a Prefab that contains the script ‘’, which does not derive from MonoBehaviour. This is not allowed.
Please change the script to derive from MonoBehaviour or remove it from the GameObject ‘bullet’.
UnityEditor.EditorApplication:Internal_CallUpdateFunctions ()” yet it has MonoBehavior

In addition to all those it gives me The namespace ‘’ already contains a definition for ‘Target’ and Type ‘Target’ already defines a member called ‘TakeDamage’ with the same parameter types