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

Question error CS1061, Pls Help :) (SOLVED)

Discussion in 'Scripting' started by mlgsnoops, Jun 25, 2020.

  1. mlgsnoops

    mlgsnoops

    Joined:
    Jun 6, 2020
    Posts:
    18
    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?)






    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Gun : MonoBehaviour
    4. {
    5.     public float damage = 10f;
    6.     public float range = 100f;
    7.  
    8.     public Camera fpsCam;
    9.     // Update is called once per frame
    10.     void Update()
    11.     {
    12.         if(Input.GetButtonDown("Fire1")){
    13.             Shoot();
    14.         }
    15.     }
    16.     void Shoot (){
    17.         RaycastHit hit;
    18.         if(Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
    19.         {
    20.             Debug.Log(hit.transform.name);
    21.             Target target = hit.transform.GetComponent<Target>();
    22.             if(target != null)
    23.             {
    24.                 target.TakeDamage(damage);
    25.             }
    26.         }
    27.     }
    28. }
    29.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Well does your
    Target
    class have a method called
    TakeDamage
    or not?
     
  3. mlgsnoops

    mlgsnoops

    Joined:
    Jun 6, 2020
    Posts:
    18
    Yes I do
     
  4. mlgsnoops

    mlgsnoops

    Joined:
    Jun 6, 2020
    Posts:
    18
    Code (CSharp):
    1. public void TakeDamage(float amount){
    2.         health -=amount;
    3.         if(health <= 0f)
    4.         {
    5.             Die();
    6.         }
    7.         void Die ()
    8.         {
    9.             Destroy(gameObject);
    10.         }
    11.  
    12.     }
    13. }
    14.  
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Which class is this method in?
     
  6. mlgsnoops

    mlgsnoops

    Joined:
    Jun 6, 2020
    Posts:
    18
    Sorry im new to scripting and i dont know what that means, can you explain it?
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Where is that TakeDamage method? Which file is it in? Can you show the whole file?
     
  8. mlgsnoops

    mlgsnoops

    Joined:
    Jun 6, 2020
    Posts:
    18
    File in unity or on my file explorer
     
  9. mlgsnoops

    mlgsnoops

    Joined:
    Jun 6, 2020
    Posts:
    18
    upload_2020-6-24_20-51-33.png

    OR

    upload_2020-6-24_20-52-9.png
     
  10. mlgsnoops

    mlgsnoops

    Joined:
    Jun 6, 2020
    Posts:
    18
    Really need help on this, this is a huge part of my game
     
  11. mlgsnoops

    mlgsnoops

    Joined:
    Jun 6, 2020
    Posts:
    18
    Fixed nvm
     
  12. EXTREME_S

    EXTREME_S

    Joined:
    Jul 24, 2020
    Posts:
    10
    How did you fix it cause I have the same problem
     
  13. TheMurphInfinity

    TheMurphInfinity

    Joined:
    Oct 3, 2020
    Posts:
    1
    same. if you have fixed it, would you please tell how?
     
  14. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    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/ass...3-syntax-error-expected.1039702/#post-6730855

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

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/