- Home /
What will happen if I GetComponent in this way? (Example)
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.
Answer by Chronos-L · Apr 10, 2013 at 09:30 AM
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;
}
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
How to use "GetComponent" and "transform" code in dll file? 0 Answers
Setting Scroll View Width GUILayout 1 Answer
Component is null? 1 Answer
Help| Convert a javascript to C# 1 Answer