What will happen when I do this or that?
I have seen too many users getting errors and null references in GetComponent()
. GetComponent()
itself is pretty straight forward: get a particular component in a game object. However, there are 3 overloads for GetComponent()
:
GetComponent( type: string )
GetComponent( type: Type )
GetComponent< T >()
and there are slight variations for the result between writing in C# and uJS. So, occasionally, users will hit problem when attempting to use GetComponent()
I have compiled scripts on all the possible combinations (I think I did, feel free to point out if I didn’t) and test out myself what will happen when one write GetComponent()
in that particular sentence/syntax.
Reminder
This is not a dicussion on which GetComponent()
is faster
Extra
Explanation/example on GetComponent: http://unitygems.com/script-interaction1/
No test ran on Boo
because I don’t know how to write in Boo
, sorry about that.
#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>()
}
}
#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;
}