Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

'Target' does not contain a definition for 'TakeDamge' and no accessible extension method 'TakeDamg

edited October 26 in Programming

I was following brackeys shooting with raycast tutorial and there was a error


 'Target' does not contain a definition for 'TakeDamge' and no accessible extension method 'TakeDamge' accepting a first argument of type 'Target' could be found (are you missing a using directive or an assembly reference?)


code:


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.TakeDamge(damage);

      }

    }

  }

}



other code:


using UnityEngine;


public class Target: MonoBehaviour

{

  public float health = 100f;


  public void TakeDamage(float amount)

  {

    health -= amount;

    if (health <= 0f)

    {

      Die();

    }

  }


  void Die()

  {

    Destroy(gameObject);

  }

}



what should I do?

Answers

Sign In or Register to comment.