Search Unity

GetComponent returns null when the component exists

Discussion in 'Scripting' started by Cjmarkham, Jul 25, 2014.

  1. Cjmarkham

    Cjmarkham

    Joined:
    May 30, 2014
    Posts:
    41
    I am trying to get a component on the same game object but it keeps returning null.

    Script 1 and a CharacterMotor are both attached to 1 game object. In Script 1 I am using the following to get the component.

    Code (CSharp):
    1. public CharacterMotor motor;
    2.  
    3. void Start () {
    4.     motor = GetComponent<CharacterMotor> ();
    5.     Debug.Log(motor); // Always null
    6. }
    7.  
    But motor is always null. I even require the component for this script and it doesn't throw any errors using

    Code (CSharp):
    1. [RequireComponent(typeof(CharacterMotor))]
     
  2. S-dieters

    S-dieters

    Joined:
    Aug 27, 2013
    Posts:
    42
    im not sure if it is the problem, but try removing the space between <> and ()
     
  3. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    Can you show a screenshot of the GameObject's Inspector?
     
  4. Pysassin

    Pysassin

    Joined:
    Dec 27, 2012
    Posts:
    87
    I know it shouldn't make a difference but have you tried calling gameObject.GetComponent?
     
  5. Cjmarkham

    Cjmarkham

    Joined:
    May 30, 2014
    Posts:
    41
    I have tried using gameObject.GetComponent, this.GetComponent, this.gameObject.GetComponent. Nothing seems to work.

    Here is the screenshot:

     
  6. Ereous

    Ereous

    Joined:
    Aug 29, 2012
    Posts:
    163
    Your using c# to access unityScript. You might need to do that old trick with checking which files are compiled first. Or don't mix the two which is a better idea. Someone actually wrote a c# version of that character motor.
     
  7. knkg17

    knkg17

    Joined:
    Dec 7, 2013
    Posts:
    2
    Vould it be because it's a java-script and you're trying to use it in c#?

    Bah, beaten by 3 mins >_<
     
  8. Cjmarkham

    Cjmarkham

    Joined:
    May 30, 2014
    Posts:
    41
    Thanks for the heads up. I have replaced the CharacterMotor script with a C# equivalent but GetComponent is still returning null.
     
  9. Ereous

    Ereous

    Joined:
    Aug 29, 2012
    Posts:
    163
    I don't know why this would help but you can do:

    Code (CSharp):
    1. // Be sure to of changed the Name of the Script here..
    2. public CSharpCharacterMotor motor;
    3. // Assigning is ok in awake.
    4. void Awake () {
    5.     motor = this.GetComponent(typeof(CSharpCharacterMotor)) as CSharpCharacterMotor;
    6.     Debug.Log(motor); // Always null
    7. }
     
  10. Cjmarkham

    Cjmarkham

    Joined:
    May 30, 2014
    Posts:
    41
    Still returning null :(

    Code (CSharp):
    1. private CharacterMotor2 motor;
    2.  
    3. void Awake () {
    4.     motor = this.GetComponent(typeof(CharacterMotor2)) as CharacterMotor2;
    5.     Debug.Log (motor);
    6. }
     
  11. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    I've sometimes found GetComponent to fail in Awake depending on order of initialisation or something. I'm pretty sure it's meant to work, but I'm pretty sure in some cases it has failed. Put it back in Start and, now that you're using a C# component, I think you should be right.
     
  12. coreyZambonie

    coreyZambonie

    Joined:
    Jan 15, 2015
    Posts:
    1
    Did you ever figure this out? I'm having the same issue with a child component not being accessible in Start().
     
  13. xander-ish

    xander-ish

    Joined:
    Jan 11, 2016
    Posts:
    1
    I have the same issue, I don't know what's wrong. I'm using the "GetComponent(CharacterMotor)"/ "GetComponent<CharacterMotor>();" in another script in the Start function and it works very well. The only solution I've found is to make a public variable of type CharacterMotor and drag the player(not the script, it will not work) in the inspector, or the object that uses that script.
     
  14. The_Elemental_of_Destruction

    The_Elemental_of_Destruction

    Joined:
    Feb 12, 2019
    Posts:
    3
    Perhaps the problem is that you DECLARED the motor, but never set it?
    Code (CSharp):
    1. public CharacterMotor motor;
    Because that is what it looks like to me. It's like how just putting

    Code (CSharp):
    1. public string empty;
    Would be null until a value was assigned to it.
     
  15. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Why necro a post from 2014/2016? Seriously doubt they are still waiting to get their motor script working...
     
    dbdenny likes this.