GetComponent doesnt find the UnityUI Image comp.

The UnityUI Image component isn’t derived from Monobehaviour, Component nor is it an interface, how do I access it in my scripts then? I tried .GetComponent, but it tells me it cant do that because of the statement above :confused:

ArgumentException: GetComponent requires that the requested component 'Image' derives from MonoBehaviour or Component or is an interface.
UnityEngine.GameObject.GetComponent[T] () (at <05f2ac9c8847426992765a22ef6d94ca>:0)
GUIController.Start () (at Assets/UI/Scripts/GUIController.cs:41)

The code I use:

Image menueImage1 = menueButton1.transform.Find("Cap").gameObject.GetComponent<Image>();

Unity Version: 2019.3.10f1

Location of ‘component’ in local Docs: Unity/2019.3.10f1/Editor/Data/Documentation/en/Manual/script-Image.html
I attached a picture of the ‘component’.

Did you perhaps want to use the UnityEngine.UI namespace?
https://docs.unity3d.com/Packages/com.unity.ugui@1.0/api/UnityEngine.UI.html

GetComponent should work.

2 Likes

Yeah it sounds like you have an incorrect “using” statement, so you’re using the Image class from the wrong namespace. Could you share all of the “using” statements at the top of your script?