Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

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

Discussion in 'Scripting' started by Jamalinjay, Aug 13, 2019 at 1:31 PM.

  1. Jamalinjay

    Jamalinjay

    Joined:
    Jul 27, 2019
    Posts:
    12
    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,022
  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_I

    GameDevCouple_I

    Joined:
    Oct 5, 2013
    Posts:
    1,740
    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:
    12
  6. Jamalinjay

    Jamalinjay

    Joined:
    Jul 27, 2019
    Posts:
    12
    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:
    12
    The script I'm trying to get its component is a Monobehaviour.. But the Java Script is in GameObject.
     
    Last edited: Aug 13, 2019 at 11:28 PM
  8. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    5,959
  9. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    14,277
    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 at 12:40 AM
    lordofduct, Lysander and Joe-Censored like this.
  10. Jamalinjay

    Jamalinjay

    Joined:
    Jul 27, 2019
    Posts:
    12
    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,022
    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:
    12
    Thanks it worked out.