Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Problem with GetComponent C#

Discussion in 'Scripting' started by Perimetric, Mar 13, 2015.

  1. Perimetric

    Perimetric

    Joined:
    Jan 24, 2015
    Posts:
    15
    I want to disable the script "First Person Controller" of my FPSController but i got this error :

    Code (CSharp):
    1. Assets/Scripts/MainScript.cs(19,53): error CS0246: The type or namespace name `FirstPersonController' could not be found. Are you missing a using directive or an assembly reference?
    Here is my code :

    Code (CSharp):
    1. public GameObject player;
    2.  
    3.     void Start () {
    4.  
    5.         FirstPersonController fpc = player.GetComponent <FirstPersonController> ();
    6.     }
    7.  
    8.     void Update () {
    9.  
    10.         fpc.enabled = false;
    11.     }
    I am using Unity 5

    Sorry for my bad english
     
  2. Roderyk

    Roderyk

    Joined:
    Mar 5, 2015
    Posts:
    75
    Firstly, define your variable outside of the Start function. (FirstPersonController fpc) ;
    Second, change Start for Awake. and do (fpc = gameObject.GetComponent<FirstPersonController>(); )
    Third, if GameObject player has a gameComponente of the type FirstPersonController, it should work.
     
    ivan866 likes this.
  3. Perimetric

    Perimetric

    Joined:
    Jan 24, 2015
    Posts:
    15
    Code (CSharp):
    1. public GameObject player;
    2. public Component fpc;
    3.  
    4.     void Awake () {
    5.  
    6.         player = this.gameObject;
    7.         fpc = player.GetComponent<FirstPersonController>();
    8.     }
    9.  
    10.     void Update () {
    11.        
    12.         fpc.enabled = false;
    13.     }
    Doesn't work .... same error ....
     
  4. bigdaddy

    bigdaddy

    Joined:
    May 24, 2011
    Posts:
    153
    Is FirstPersonController in a namespace? If so, then you need to add
    Code (CSharp):
    1. using what_ever_namespace_FirstPersonController_is_in
     
  5. Roderyk

    Roderyk

    Joined:
    Mar 5, 2015
    Posts:
    75
    If this doesn't work, show us your palyer gameObject (from the inspector)
     
  6. Perimetric

    Perimetric

    Joined:
    Jan 24, 2015
    Posts:
    15
    " using what_ever_namespace_FirstPersonController_is_in " doesn't work.....

    My FPSController :
     
  7. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
    is the MainScript.cs also added to the FPSController GameObject?

    Edit:

    also check: open up the FirstPersonController Script and check on of the first lines to be

    Code (CSharp):
    1. public class FirstPersonController : MonoBehaviour {
     
  8. Roderyk

    Roderyk

    Joined:
    Mar 5, 2015
    Posts:
    75
    If you want to disable this Script inside the FPSController (you need to assign a Tag to it) from another Script, you need first to get de FPSController GameObject.

    Code (CSharp):
    1. fpsController = GameObject.FindWithTag("FPSControllerSelectedTagName");
    2. FirstPersonController fpc = fpsController.GetComponent<FirstPersonController>();
     
  9. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,752
    You should write the name of the namespace, the code you've quoted was just an example, obviously.

    Open up the script of the FirstPersonController class, there should be a line with 'namespace ....'.
    Copy what's behind it and add 'using ...' and what you've copied to your current file.

    The FPS Controller which is shipped with the current version uses the namespace
    'UnityStandardAssets.Characters.FirstPerson'.

    Thus, the line to be added should be
    Code (CSharp):
    1. using UnityStandardAssets.Characters.FirstPerson;
    in your file that needs to use the type.
     
    Sulemanalimalik and tobad like this.
  10. Perimetric

    Perimetric

    Joined:
    Jan 24, 2015
    Posts:
    15
    Thanks, it works ;)
     
unityunity