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. Dismiss Notice

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
    Hi guys my gun script is showing error that the type or namespace 'Target' cannot be found.
    Pls help me and tell 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. LilFire

    LilFire

    Joined:
    Jul 11, 2017
    Posts:
    74
    Whether there is no Target class in your project or this class is in a specific namespace and you should add:
    Code (CSharp):
    1. using YourNamespace;
     
  3. aaronghosh14A

    aaronghosh14A

    Joined:
    Nov 9, 2019
    Posts:
    13
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637