- Home /
Beta 4.6 - Cannot get Image Component
I am currently working on an Android Project with the vuforia AR engine. I wanted to use the new UI engine to overlay a menu which can be toggled. In a seperate project I created the menu to get to know the new UI functions. The menu has some toggle Buttons which change the Icon when pressed. For example when I turn off the sound, the sprite is changed to a "sound off" Icon.
All other functions, like showing/hiding the buttons, muting/unmuting sound and even toggling the flashlight on/off are working as expected.
In the Test-Project everything worked out fine. But when I rebuilt the menu in my game, using the same scripts and Objects, I get the following Error: The type Image' must be convertible to
UnityEngine.Component' in order to use it as parameter T' in the generic type or method
UnityEngine.Component.GetComponent()'
Here is a C# code sample which works fine in a seperate project, but is throwing an error in my game:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class TestImageComponent : MonoBehaviour {
// Use this for initialization
void Start () {
Image img = GetComponent<Image>();
Debug.Log(img.sprite);
}
}
In both projects I create a canvas, add an Image, add a UI Sprite to the image and add the script.
Both projects import UnityEngine.UI. My first guess was that I missed some Imports
I rebuilt the menu by hand. When this failed I exported a unitypackage from the working test Project. In both cases I get the same error.
I am suspecting this to be a bug, but maybe some of you had similar problems, or better, a solution?
Thank you, CanisLupus! Vuforia seems to have an Image class defined, so I followed your suggestion, added the following line to my script and adapted the type where necessary:
using UiImage = UnityEngine.UI.Image;
...... Inside the class .....
buttonImage = GetComponent<UiImage>();
Please convert your comment to an answer so I can mark it as correct.
Note there is an open thread in the Vuforia forum, to which I just replied. You might want to monitor it for any updates:
https://developer.vuforia.com/forum/issues-and-bugs/qualcomm-vuforia-conflict-unity-46
EDIT: They already replied: the issue is fixed in Vuforia 4.0 (currently Beta).
Answer by CanisLupus · Nov 02, 2014 at 02:22 PM
Is it possible that you have another class named Image
somewhere in your game? If such a class is located in the default namespace, it might be conflicting with Unity's Image class, and the compiler only sees your class, which is not a Component.
Try adding the following after your usings and see if the problem remains:
using Image = UnityEngine.UI.Image;
Update:
I'm glad this solved the problem ;)
For others reading this, please also read habsi70's comment below his question, as it completes my answer. Trying to redefine Image like I did can result in other compilation errors, so use a new name (ex.: UiImage).
this is a great fix. it even fixed the problem of it not showing in the inspector, serialized, or the public attribute.