Im making a game were I want my background image to change between hundreds of different images that I have in my assets folder. Is there a way to do this by code? I have my Image “testImage” decleared in my script and I have dragged the Image gameobject to the slot in inspector. In my project assets I have the other source image that I want to change to called “NewSourceImage”. I have tried with this code but it doesnt work:
public Image testImage;
public void Change()
{
testImage = GetComponent<Image>("NewSourceImage");
}
I get the error message: error CS0308: The non-generic method `UnityEngine.Component.GetComponent(System.Type)’ cannot be used with the type arguments
Resources.Load() is always returning an Object because it can nearly load anything, which means you need to cast the result into what the loaded resource will be.
spr = Resources.Load ("newSourceImage") as Sprite;
or
spr = (Sprite) Resources.Load ("newSourceImage");
My guess is that line 9 will also throw an error, because it does not match the preceding answer.