- Home /
BCE0019: 'controlsEnabled' is not a member of 'UnityEngine.Component'
Error on 'controlsEnabled'
function Start () {
Player = GameObject.FindWithTag("Player");
mainCamera = GameObject.FindWithTag("MainCamera");
weaponCamera = GameObject.FindWithTag("WeaponCamera");
vehicleCam.camera.enabled = false;
vehicle.GetComponent(VehicleControllScript).controlsEnabled = false;
vehicleCam.GetComponent(AudioListener).enabled = false;
Answer by Chronos-L · Apr 04, 2013 at 07:07 AM
Incorrect (Previous Answer)
GetComponent(type)
or GetComponent(string)
will return Component
, you will need to cast it to do type-specific operation; in your case, setting the bool of a script:
Correction
GetComponent(string)
will return Component
, you will need to cast it to do type-specific operation; in your case, setting the bool of a script (Assume that it is named XYZ.js):
( vehicle.GetComponent("XYZ") as XYZ).controlsEnabled = false;
Or you can use GetComponent(type)
:
vehicle.GetComponent(XYZ).controlsEnabled = false;
Another alternative is to use the generic GetComponent()
:
uJS
vehicle.GetComponent.<XYZ>().controlsEnabled = false;
C#
vehicle.GetComponent<XYZ>().controlsEnabled = false;
How to use the GetComponent (Example)
What will happen when I do this or that?*
UnityScript/uJS
You should not run the DataCheck.js as it is, you need to comment out everything but the line/snippet that you are interested in
Data.js
#pragma strict
public var data : int = 1;
DataCheck.js
#pragma strict
var gameObjectWithData : GameObject;
function Start () {
/* ====== Anonymous Variable/One-liner ====== */
/* +++++++++ GetComponent( type : string ) +++++++++++++++++++++++++++ */
/* - Compile error: data is not member of Component - */
gameObjectWithData.GetComponent("Data").data = 9;
/* - Okay - */
(gameObjectWithData.GetComponent("Data") as Data).data = 9;
/* +++++++++ GetComponent( type : Type ) +++++++++++++++++++++++++++ */
/* - Okay - */
gameObjectWithData.GetComponent(Data).data = 9;
/* +++++++++ GetComponent<T>() +++++++++++++++++++++++++++ */
/* - Okay - */
gameObjectWithData.GetComponent.<Data>().data = 9;
/* ====== Assignment ====== */
/* +++++++++ GetComponent( type : string ) +++++++++++++++++++++++++++ */
/* - x is considered a component, so this line is still okay - */
var x = gameObjectWithData.GetComponent("Data");
/* - Not okay, Compile error: data is not member of Component - */
x.data = 9;
/* - Warning: Implicit downcast from Component to Data - */
var x : Data = gameObjectWithData.GetComponent("Data");
x.data = 9;
/* - Okay - */
var x : Data = gameObjectWithData.GetComponent("Data") as Data;
x.data = 9;
/* +++++++++ GetComponent( type : Type ) +++++++++++++++++++++++++++ */
/* - Okay - */
var x = gameObjectWithData.GetComponent(Data);
x.data = 9;
/* - Okay - */
var x : Data = gameObjectWithData.GetComponent(Data);
x.data = 9;
/* +++++++++ GetComponent<T>() +++++++++++++++++++++++++++ */
/* - Okay - */
var x = gameObjectWithData.GetComponent.<Data>();
x.data = 9;
/* - Okay - */
var x : Data = gameObjectWithData.GetComponent.<Data>();
x.data = 9;
}
C#
You should not run the DataCheckCS.cs as it is, you need to comment out everything but the line/snippet that you are interested in
DataCS.cs
using UnityEngine;
public class DataCS : MonoBehaviour {
public int data = 1;
}
DataCheckCS.cs
using UnityEngine;
public class DataCheckCS : MonoBehaviour {
public GameObject gameObjectWithDataCS;
void Start () {
/* ====== Anonymous Variable/One-liner ====== */
/* +++++++++ GetComponent( type : String ) +++++++++++++++++++++++++++ */
/* - CS1061, UnityEngine.Component doesn't have `data` ... - */
gameObjectWithDataCS.GetComponent("DataCS").data = 9;
/* - Okay - */
(gameObjectWithDataCS.GetComponent("DataCS") as DataCS).data = 9;
((DataCS)gameObjectWithDataCS.GetComponent("DataCS")).data = 9;
/* +++++++++ GetComponent( type : Type ) +++++++++++++++++++++++++++ */
/* - CS0119, expression denotes a `type` ... - */
gameObjectWithDataCS.GetComponent(DataCS).data = 9;
( gameObjectWithDataCS.GetComponent(DataCS) as DataCS ).data = 9;
( (DataCS) gameObjectWithDataCS.GetComponent(DataCS)).data = 9;
/* - CS1061, UnityEngine.Component doesn't have `data` ... - */
gameObjectWithDataCS.GetComponent(typeof(DataCS)).data = 9;
/* - Okay - */
( gameObjectWithDataCS.GetComponent(typeof(DataCS)) as DataCS ).data = 9;
( (DataCS) gameObjectWithDataCS.GetComponent(typeof(DataCS))).data = 9;
/* +++++++++ GetComponent<T>() +++++++++++++++++++++++++++ */
/* - Okay - */
gameObjectWithDataCS.GetComponent<DataCS>().data = 9;
/* ====== Assignment ====== */
/* +++++++++ GetComponent( type : string ) +++++++++++++++++++++++++++ */
/* - CS0226, Cannot implicitly convert type ... - */
DataCS x = gameObjectWithDataCS.GetComponent("DataCS");
/* - Okay - */
DataCS x = gameObjectWithDataCS.GetComponent("DataCS") as DataCS;
x.data = 9;
/* - Okay - */
DataCS x = (DataCS)gameObjectWithDataCS.GetComponent("DataCS");
x.data = 9;
/* +++++++++ GetComponent( type : Type ) +++++++++++++++++++++++++++ */
/*Refer to Anonymous Variable/One-liner->GetComponent( type : Type )*/
/* +++++++++ GetComponent<T>() +++++++++++++++++++++++++++ */
/*Refer to Anonymous Variable/One-liner->GetComponent<T>()*/
}
}
using uJS result:
The name 'VehicleControllScript' does not denote a valid type ('not found')
If it is WehicleScript.js with a W, then the code would be:
vehicle.GetComponent.<WehicleScript>().controlsEnabled = false;
It does not affect using that one
I am not sure why it doesn't work. Can you make sure that your script looks somewhat like this:
WehicleScript.js
var controlsEnabled : boolean = true;
...
The script you used in the question
... //This is no longer needed, just delete it var VehicleControllScript : String = "ScriptName"; ...
function Start () {
...
//in the <>, you insert the class name, which is not a variable, just type it in as it is
vehicle.GetComponent.<WehicleScript>().controlsEnabled = false;
...
}
This answer is incorrect, given that the question is using Unityscript, not C#. GetComponent does not return Component in Unityscript, unless you use strings. It returns the type of the component you're getting. Also, Unityscript doesn't do C#-type casting with "()", it uses "as". There's no reason to use the generic version of GetComponent in Unityscript, since the non-generic version does the same thing, except slightly faster and less ugly.
(Unless Xdy is using a version of Unity older than 3.4, in which case this answer is correct, but if so, there's really no reason for using such an old version of Unity at this point.)
I made some premature assumptions when I saw vehicle.GetComponent(VehicleControllScript).controlsEnabled
, I assumed that VehicleControllScript
is the class name and I came to the conclusion that like GetComponent(string)
, GetComponent(type)
also returns Component
(I also check the Documentation, it is stated there that function GetComponent (type : Type) : Component
). However, later, Xdy shows that the VehicleControllScript
is in fact a string. So, I made a mistake on GetComponent(type)
.
I have to apologize for using ()
to cast, that wouldn't work in uJS, its been a while since I wrote in uJS.