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
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

'enabled' is not a member of 'UnityEngine.Component'. .

Discussion in 'Scripting' started by Jamalinjay, Aug 13, 2019.

  1. Jamalinjay

    Jamalinjay

    Joined:
    Jul 27, 2019
    Posts:
    14
    Please Someone should help me with this

    Code (JavaScript):
    1. var CarControl : GameObject;
    2.  
    3. function Start () {
    4.    CarControl.GetComponent("CarController").enabled = true;
    5.  
    6. }
    7.  
    8.  
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,734
  3. hlw

    hlw

    Joined:
    Aug 12, 2017
    Posts:
    250
    I believe it doesn't know as is it is a behaviour and it might require casting.
    In Csharp it would only require you to do:
    CarControl.GetComponent<CarController>().enabled = true;
    or (CarControl.GetComponent("CarController") as CarController).enabled = true;

    Not sure about js.
     
    Jamalinjay likes this.
  4. GameDevCouple

    GameDevCouple

    Joined:
    Oct 5, 2013
    Posts:
    2,449
    why are you writing code in Javascript? Is no longer supported

    Is this a monobehaviour? If not then it wont necessarily have a enabled property.
     
    StarManta likes this.
  5. Jamalinjay

    Jamalinjay

    Joined:
    Jul 27, 2019
    Posts:
    14
  6. Jamalinjay

    Jamalinjay

    Joined:
    Jul 27, 2019
    Posts:
    14
    I've tried it's not still working.. It's still unable to get the component "CarController "
     
  7. Jamalinjay

    Jamalinjay

    Joined:
    Jul 27, 2019
    Posts:
    14
    The script I'm trying to get its component is a Monobehaviour.. But the Java Script is in GameObject.
     
    Last edited: Aug 13, 2019
  8. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    8,315
  9. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    15,732
    C# and JS scripts can't communicate with each other out of the box. In order for C# and JS scripts to communicate with each other you need to go through a series of steps to make C# scripts compile before JS scripts. That said this isn't a foolproof solution as some scripts will move just fine while others won't like being moved.

    Additionally this is not a two-way solution. You can either have JS call C# or C# call JS but not both at the same time.

    https://stackoverflow.com/questions...p-function-from-unityscript/23217923#23217923

    Therefore the real solution here is to stop using a language that has been discontinued by Unity. Below is the script you need written in C#. Just shove it into a file name "CarControlActivator.cs" and use it like you wanted to use the JS script.

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class CarControlActivator: MonoBehaviour
    4. {
    5.     public GameObject CarControl;
    6.  
    7.     void Start()
    8.     {
    9.         CarControl.GetComponent<CarController>().enabled = true;
    10.     }
    11. }
    12.  
     
    Last edited: Aug 14, 2019
  10. Jamalinjay

    Jamalinjay

    Joined:
    Jul 27, 2019
    Posts:
    14
    It's giving me the error " The type name space 'CarController' could not be found. Are you missing 'Unity Standard Assets. Vehicles. Car' using directives?"
     
  11. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,734
    Well, that's because you're missing 'Unity Standard Assets.Vehicles.Car' using directives. Add them at the top of your script and it should work.
     
  12. Jamalinjay

    Jamalinjay

    Joined:
    Jul 27, 2019
    Posts:
    14
    Thanks it worked out.
     
unityunity