- Home /
How to change an images source image from script? C#
I have been stuck on this for several hours now and I cant find any answer that I understand here or on google:/. Being a beginner at Unity and C# please try to explain for an idiot:)
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".
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
Ok, thanks but I tried as you wrote and get this error message now:
error CS0308: The non-generic method `UnityEngine.Component.GetComponent(System.Type)' cannot be used with the type arguments
You're passing type parameters to a method that is not generic. Update your question with the changes you made to your code, or we have no chance to fix it. This isn't Delphi, and I'm not the oracle of it. :)
Answer by CHPedersen · Nov 25, 2014 at 08:34 AM
In C#, there should be no period before the type parameter brackets:
testImage = GetComponent<Image>("NewSourceImage");
Your answer
![](https://koobas.hobune.stream/wayback/20220613172357im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
is this correct way to change var from other script? 1 Answer
Is it possible to change an images source image by code? C# 2 Answers
The variables and functions 2 Answers
Any runtime image file browsers available? 1 Answer
Changing GameObject texture? 4 Answers