Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

error CS1061: 'Target' does not contain a definition for 'TakeDamage' and no accessible extension me

Discussion in 'Scripting' started by EXTREME_S, Jul 25, 2020.

  1. EXTREME_S

    EXTREME_S

    Joined:
    Jul 24, 2020
    Posts:
    10
    I need help on getting rid of that error here is my code

    The Gun code

    using UnityEngine;

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

    public Camera MainCamera;

    // Update is called once per frame
    void Update()
    {
    if (Input.GetButtonDown("Fire1"))
    {
    Shoot();
    }
    }

    void Shoot()
    {
    RaycastHit hit;
    if (Physics.Raycast(MainCamera.transform.position, MainCamera.transform.forward, out hit, range))
    {
    Debug.Log(hit.transform.name);

    Target target = hit.transform.GetComponent<Target>();

    if (target != null) ;
    {
    target.TakeDamage(damage);
    }
    }
    }
    }

    The Target code

    using UnityEngine;

    public class Target : MonoBehaviour
    {
    public float health = 50f;

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



    void Die()
    {
    Destroy(gameObject);
    }
    }
     
  2. Elango

    Elango

    Joined:
    Jan 27, 2016
    Posts:
    107
    Did you save the Target.cs file?
     
  3. EXTREME_S

    EXTREME_S

    Joined:
    Jul 24, 2020
    Posts:
    10
    Omg Thank you so much I am such a noob lmao