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')
– XdyWhat 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.
– Chronos-LIf it is WehicleScript.js with a W, then the code would be: vehicle.GetComponent.<WehicleScript>().controlsEnabled = false;
– Chronos-LYucky FPS Kit, bad naming of scripts.
– EliteMossyIt 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'.
– Xdy