GetComponent returns error although the component referenced exists

This code

GameObject.Find("Background").GetComponent<Image>().sprite = Resources.Load<Sprite>("images/bggamestall");

returns the following error:

ArgumentException: GetComponent requires that the requested component 'Image' derives from MonoBehaviour or Component or is an interface.
UnityEngine.GameObject.GetComponent[T] () (at <bdd20210bb844b2e88e1149ea99da5ef>:0)
TEST.Start () (at Assets/scripts/TEST.cs:16)

However, the object Background referenced is a UI image object, so Image is a component under it. The required modules and libraries have already been included.

You’re reading the error wrong. It’s complaining that Image isn’t a type that inherits from Component, though UnityEngine.UI.Image does inherit from Component, so I’m not sure why this error is occuring.

You don’t have your own Image class somewhere?

There are multiple classes called “Image” in various different libraries. Either you’ve got an Image class in your code, or you’ve got a “using” statement at the top of the file for another namespace which does, and the compiler is looking at the wrong Image class as a result.

So, first, check your “using” statements to make sure there’s no unnecessary junk being included by accident. Modern code editors often try to be helpful by including namespaces they think you need, but they can be a little trigger happy.

Sometimes you need to have multiple namespaces where classes have the same name. In those cases you can specify which class you want by writing the fully qualified name, as spiney did above, i.e. “UnityEngine.UI.Image” instead of just “Image”.

Could it be using the UI Toolkit Image?