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;

1 Answer

1

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')

What is the name of the script? Is it VehicleControllScript.js with double L? I just copy-paste the code as I do not know how you name the .js file.

If it is WehicleScript.js with a W, then the code would be: vehicle.GetComponent.<WehicleScript>().controlsEnabled = false;

Yucky FPS Kit, bad naming of scripts.

It does not affect using that one I also try to change the upper part of the script to WehicleScript and it tells Unknown identifier: 'VehicleControllScript'.