Search Unity

The name `GetComponent' does not exist in the current context

Discussion in 'Multiplayer' started by Eiseno, Nov 19, 2015.

  1. Eiseno

    Eiseno

    Joined:
    Nov 1, 2015
    Posts:
    86
    Hi,
    I am working on this code.I take this GetComponent code from other example.in example code its working but in my code i take The name `GetComponent' does not exist in the current context error.How can i solve this ?
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4. using UnityEngine.UI;
    5.  
    6.  
    7. [RequireComponent(typeof(SteamVR_TrackedObject))]
    8. public class Player_Shoot : NetworkBehavior {
    9.     private float damage = 25;
    10.     private float range = 200;
    11.     [SerializeField]
    12.     private Transform camTransform;
    13.     private RaycastHit hit;
    14.     // Use this for initialization
    15.     SteamVR_TrackedObject trackedObj;
    16.     void Awake()
    17.     {
    18.         trackedObj =  GetComponent<SteamVR_TrackedObject>();
    19.        
    20.     }
    21.  
    22.     void Start () {
    23.      
    24.  
    25.     }
    26.  
    27.  
    28.     // Update is called once per frame
    29.     void FixedUpdate () {
    30.        
    31.         CheckIfShooting();
    32.     }
    33.  
    34.     private void CheckIfShooting()
    35.     {
    36.         if (true)
    37.         {
    38.             return;
    39.         }
    40.         else
    41.         {
    42.             var device = SteamVR_Controller.Input((int)trackedObj.index);
    43.             if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger))
    44.             {
    45.                 Shoot();
    46.                 Debug.Log("Tuşa bastı");
    47.             }
    48.         }
    49.     }
    50.  
    51.     void Shoot()
    52.     {
    53.         if (Physics.Raycast(camTransform.TransformPoint(0,0,0.5f),camTransform.forward,out hit,range))
    54.         {
    55.             Debug.Log(hit.transform.tag);
    56.             if (hit.transform.tag == "Enemy")
    57.             {
    58.                 string uIdentity = hit.transform.name;
    59.                 CmdTellServerWhoWasShot(uIdentity, damage);
    60.             }
    61.         }
    62.     }
    63.     [Command]
    64.      void CmdTellServerWhoWasShot(string uIdentity, float damage)
    65.     {
    66.         GameObject go = GameObject.Find(uIdentity);
    67.         //Apply Damage to zombie
    68.     }
    69. }
    70.  
     
  2. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    NetworkBehaviour not NetworkBehavior
     
    Eiseno likes this.