Image' does not contain a definition for 'texture' and no accessible extension method 'texture' accepting a first argument of type 'Image'

Viewed 4002

This error is constantly popping up in Unity after I imported a project.

This is the code :

public static void CameraFadeSwap(Texture2D texture){
    if(cameraFade){
        cameraFade.GetComponent<Image>().texture=texture; 
    }
}

The error is:

Assets\Scripts\Utils\iTween.cs(6016,37): error CS1061: 'Image' does not contain a definition for 'texture' and no accessible extension method 'texture' accepting a first argument of type 'Image' could be found (are you missing a using directive or an assembly reference?)

How can it be fixed?

1 Answers

Well it doesn't (see Image).


But I think what you are trying is assign a new Image.sprite instead.

Therefore you'll need to create a new sprite from that texture using Sprite.Create

public static void CameraFadeSwap(Texture2D texture)
{
    if(cameraFade)
    {
        var newSprite = Sprite.Create(texture, new Rect(0.0f, 0.0f, texture.width, texture.height), Vector2.one * 0.5f);
        cameraFade.GetComponent<Image>().sprite = newSprite;
    }
}
Related