Search Unity

JS get component

Discussion in 'Editor & General Support' started by BlakeGillman, Apr 24, 2014.

  1. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
    The problem is so simple its really making me mad.

    The game is not detecting this component on my car and its a big problem.

    Code (csharp):
    1.  
    2. public var theCar : GameObject; /* The car */
    3.  
    4. theCar.gameObject.transform.GetComponent(carMovement).active = true;
    5.  
    Of course this is not all the code but you see what I mean.

    I get no error from the log, just it simply does not make the script 'carMovement'.js become active.
     
  2. rutter

    rutter

    Joined:
    Mar 21, 2012
    Posts:
    84
    I see a few possible errors:

    1. theCar could be null
    2. GetComponent could return null
    3. Components usually have "enabled" instead of "active"
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Always, actually. .active is only for GameObjects, or I should say used to be; it's obsolete now, and you'd use SetActive instead.

    --Eric
     
  4. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
    Assets/Game/GlobalScripts/Vehicles/Land/Car.js(66,71): BCE0019: 'SetActive' is not a member of 'carMovement'.

    Code (csharp):
    1.  
    2. theCar.gameObject.transform.GetComponent(carMovement).SetActive = true;
    3.  
     
  5. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
    What do you mean GetComponent could return null? Its simply getting the component from theCar

    theCar is not null.
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Sorry, you misunderstood. I wasn't telling you to use SetActive...might want to read my post again (plus the post I was responding to).

    --Eric
     
  7. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,052
    It probably should be:
    Code (csharp):
    1.  
    2. public var theCar : GameObject; /* The car */
    3.  
    4. theCar.GetComponent(carMovement).enabled = true;
    5.  
    But that will only make that component active. What happens from there will depend on the component.
     
  8. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    So, you have a script called carMovement, and you are trying to control whether that script updates or not. I can't tell if your problem is that you cannot get a reference to your script.