I’m having some trouble. So I have a canvas with an object I called BackGround on it, that just has an Image. I wanted to script to change out the image based on whatever conditions.
Problem is that I can’t seem to find a way to change the image when the only way seems to be: GetComponent().sprite = NewSprite; however when I’ve used that I get an Argument Exception:
ArgumentException: GetComponent requires that the requested component ‘Image’ derives from MonoBehaviour or Component or is an interface.
UnityEngine.Component.GetComponent[T] () (at :0)
Anyone know what the heck I do? Or do I just suck it up and leave it be.
Sounds to me like you have another Image script that doesn’t inherit from MonoBehaviour.
In which case I would suggest changing that script name so it doesn’t conflict with Unity’s script. In which case you also need the proper using statement at the top.
Otherwise, you can declare the Image target by adding the namespace to it.
GetComponent<UnityEngine.UI.Image>()
1 Like
Hmm. Well I can guarantee there are no conflicting script names. I’m merely trying to access an Image component and change the source image using a script that I’ve attached to the object that is on the UI. I’ve already put “using UnityEngine.UIElements;” added to the top.
Is it maybe the UIElements part? It just needs to be UI?
EDIT: Yup! That was it! Wow, I feel like an idiot, thank you though!
1 Like