Premier Bromanov
Premier Bromanov

Reputation: 461

How do I get a reference to an Image in Unity 2019.3?

So normally in Unity, I can get a reference to a component on a gameobject by using GetComponent<Type>(). In this instance, I'd like to do a simple GetComponentInChildren<Image>() so I can do some code to the image. Normal unity stuff.

But in 2019.3, it seems like I can't do that, since Image isn't a component anymore.

ArgumentException: GetComponent requires that the requested component 'Image' derives from MonoBehaviour or Component or is an interface.

So I'm not sure how I would get a reference to an Image "component" on a RectTransform in order to do stuff to it. Any ideas?

Upvotes: 0

Views: 6354

Answers (1)

Premier Bromanov
Premier Bromanov

Reputation: 461

Silly answer, but I figured it out. Leaving here for posterity.

my Image type was incorrectly defined as a Unity.UIElements.Image rather than what I wanted, Unity.UI.Image. That is to say, my script was using Unity.UIElements; instead of the correct one.

Very cool to have two different classes with the same name.

Upvotes: 3

Related Questions