OK, I’m just making myself more and more confused lately. It seems like there are various solutions to referencing of components which only work under specific and as yet mysterious circumstances. If possible I’d just like to clear some of these things up.
First, a generic question;
- Why is the dot notation different between GetComponent(“Component Name”) and GetComponent.()?
Next, a few examples;
Example A
#pragma strict
function Start ()
{
GetComponent.<UI.Image>().color.a = 0.2;
}
This works as expected.
Example B
#pragma strict
function Start ()
{
GetComponent.<Image>().color.a = 0.2;
}
This doesn’t work;
(5,23): BCE0018: The name ‘Image’ does not denote a valid type (‘not found’). Did you mean ‘UnityEngine.UI.Image’?
- Why do I need to use “UI.Image” instead of just “Image”?
Example C
#pragma strict
function Start ()
{
GetComponent("Image").color.a = 0.2;
}
This doesn’t work;
(6,24): BCE0019: ‘color’ is not a member of ‘UnityEngine.Component’.
- All I changed was that I referenced it by name instead of type, why isn’t color a member any more?
Because I will later need to use the referenced Image component many times, I’d now like to store it in a variable to keep things neat and better optimised;
Example D
#pragma strict
var imageComponent;
function Start ()
{
imageComponent = GetComponent.<UI.Image>();
imageComponent.color.a = 0.2;
}
This doesn’t work;
(8,24): BCE0019: ‘color’ is not a member of ‘Object’.
But if I stick the variable declaration inside the Start() function…
Example E
#pragma strict
function Start ()
{
var imageComponent = GetComponent.<UI.Image>();
imageComponent.color.a = 0.2;
}
This works.
- Why does example D not work, but example E does?
Last example (I promise!);
Example F
#pragma strict
var imageReference;
function Start ()
{
var imageComponent = GetComponent.<UI.Image>();
imageReference = imageComponent;
imageReference.color.a = 0.2;
}
This doesn’t work too, of course;
(9,24): BCE0019: ‘color’ is not a member of ‘Object’.
- Why doesn’t this work either?
Cheers!