Figuring Out 'GetComponent' on Image

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!

going to preface this with “you really should try coding in c#” because if these things annoy you you’ll love that c# mostly forces types to be explicitly declared.

A “var” is pretty much “whatever it needs to be” type.

D and F, the var is declared without any context, what type does it need to be… “I dunno” => var is set to be a generic object. In F I would assume line 8 implicitly casts the Image type to an Object type when assigning it.

E var declared as a part of a line which has a function which returns a typed value, var is that type. The <> getcomponent returns a reference of the specified type.

Having said that, the GetComponent using a string doesn’t return a type, that returns a generic object which must then be cast to the specific type explicitly, as in C.

GetComponent("Image") as UI.Image
// I think that's unityscript casting syntax

Why do you need “UI.Image” instead of “Image”? comes down to namespaces, kinda like folders, the classes are wrapped up into namespace. In C# you can tell the compiler what namespaces you want to access at the top of the file, don’t think unityscript allows for that though.

I see your point about C#. I’m starting to think it’s actually a much better way to start out learning than JS, as in my learning experience many of the shortcuts and conveniences of JS have been the bits that made even simple principles seem more complicated than they really were.

I must admit I’m a bit lost when it comes to namespaces though. My assumption was that JS just does it all “under the hood” as the docs say, is it fair to assume that this is an exception then? Are there others?

So with the typecasting, I think I’ve got one more question then - because I guess the bit that caught me off guard was that the image component in the inspector is described as “Image (script)” I wrongly assumed the same rules apply as for scripts, which you can reference without implicit typecasting using the string method of GetComponent. What makes “Image” different from a normal script? Namespaces again?

Anyway, thanks very much for your quick answer. I know I’m still asking things, but it’s helped a huge amount already!

1 Like