Search Unity

My gun script is showing error pls help.

Discussion in 'Scripting' started by aaronghosh14A, Sep 10, 2020.

  1. aaronghosh14A

    aaronghosh14A

    Joined:
    Nov 9, 2019
    Posts:
    13
    Whenever i load my script it shows the error that the type or namespace 'Target' cannot be found.
    Can anybody pls help me where i am going wrong

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Shhot : MonoBehaviour
    4. {
    5.     public Animator anim;
    6.     public AudioSource sound;
    7.     public AudioSource sound1;
    8.     public AudioSource sound3;
    9.     public float range = 100f;    
    10.     public float damage  = 10f;                
    11.     public Camera fpsCam;      
    12.  
    13.     void Start()
    14.     {
    15.          anim = GetComponent<Animator>();
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.       if(Input.GetButtonDown("Fire1")){
    22.             anim.Play("Fire");
    23.              sound1.Play();
    24.               shoot();
    25.         }
    26.          if(Input.GetButtonUp("Fire1")){
    27.             anim.Play("Idle");
    28.         }
    29.  
    30.        if(Input.GetKeyDown("q")){
    31.             anim.Play("Knife Attack 1");
    32.            
    33.         }
    34.         if(Input.GetKeyDown("r") ){
    35.             anim.Play("Reload Ammo Left");
    36.             sound.Play();
    37.         }  
    38.         if(Input.GetKeyDown("e")){
    39.             anim.Play("Inspect");
    40.            
    41.         }
    42.         if(Input.GetButtonDown("Fire2")){
    43.             anim.Play("Aim In");
    44.             sound3.Play();
    45.         }
    46.          if(Input.GetButtonUp("Fire2")){
    47.             anim.Play("Aim Out");
    48.              sound3.Play();
    49.         }
    50.         if(Input.GetKeyDown("w") ){
    51.             anim.Play("Walk");
    52.         }
    53.  
    54.         if(Input.GetKeyUp("w")){
    55.            anim.Play("Idle");
    56.         }
    57.     }
    58.  
    59.      void shoot(){
    60.  
    61.   RaycastHit hit;
    62.   if(Physics.Raycast(fpsCam.transform.position , fpsCam.transform.forward , out hit , range ))
    63.   {
    64.       Debug.Log(hit.transform.name);
    65.  
    66.       Target target = hit.transform.GetComponent<Target>();
    67.        if(target !=null)
    68.        {
    69.           target.TakeDamage(damage);
    70.        }
    71.   }
    72. }
    73. }
    74.  
    75.  
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @aaronghosh14A
    Code (CSharp):
    1. Target target = hit.transform.GetComponent<Target>();
    Do you actually have a MonoBehaviour class Target somewhere in your project?