Update source file on an UI image object

Hello,

I am trying to update the source file on an image object inside of a canvas, but get an error when I try to use the GetComponent<> method.

I originally wrote my UpdateColor() method to work with a sprite renderer component and not working with the UI or canvas objects. And when I tried rewriting it to use the components in the Image UI object I hit a wall : (

void UpdateColor(GameObject p1, Sprite p2)
{
    //update a specific gameobject with the sprite sent as a parameter

    Debug.Log("gameobject: " + p1);
    Debug.Log("image component:" + p1.GetComponent<Image>().sprite);

    //p1.GetComponent<Image>().sprite = p2;

}

here is the object in the inspector

and the error I keep getting

Double check your imports and make sure you are actually referring to Image from UGUI library and not one of dozen classes also called Image in other libraries.

Thanks for the lead!

In my import list I had using UnityEngine.UIElements; and changing it to using UnityEngine.UI; let me use GetComponent! :slight_smile: