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?

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 :wink:

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).