- Home /
Compnent.guiTexture is obsolete
I have just reopened an old project from Unity 4, and recieved quite a few errors in Unity 5. It is my play button, and even trying to use GetComponent() doesnt seem to fix it. here are the errors.
Assets/Scripts/PlayButton.js(11,9): BCE0144: 'UnityEngine.Component.guiTexture' is obsolete. Property guiTexture has been deprecated. Use GetComponent() instead. (UnityUpgradable)
Assets/Scripts/PlayButton.js(11,20): BCE0019: 'texture' is not a member of 'UnityEngine.Component'.
Assets/Scripts/PlayButton.js(16,9): BCE0144: 'UnityEngine.Component.guiTexture' is obsolete. Property guiTexture has been deprecated. Use GetComponent() instead. (UnityUpgradable)
Assets/Scripts/PlayButton.js(16,20): BCE0019: 'texture' is not a member of 'UnityEngine.Component'.
Here is my simple script, How can i fix the errors?
#pragma strict
var playNormal : Texture2D;
var playHover : Texture2D;
function Start () {
}
function OnMouseEnter ()
{
guiTexture.texture = playHover;
}
function OnMouseExit ()
{
guiTexture.texture = playNormal;
}
function OnMouseDown ()
{
Application.LoadLevel("spawned");
}
Answer by Xarbrough · Jun 18, 2015 at 09:04 PM
using UnityEngine;
[RequireComponent(typeof(GUITexture))]
public class TestScript : MonoBehaviour
{
public Texture2D playNormal;
public Texture2D playHover;
private new GUITexture guiTexture;
void Start ()
{
guiTexture = GetComponent<GUITexture>();
}
// Rest of your code.
}
Use this code. It will automatically add a GUITexture component to your GameObject if there is none. Then it creates a variable for the GUITexture. In past years, there was an automatic property for this, but since the GUITexture and old OnGUI System is being replaced by the new Unity UI, the properties are becoming obsolete. If you really need that component for some reason, use my code. The "new" keyword will override the inherited (but obsolete) property "guiTexture". It is still in Unitys codebase for documentation and to give instruction on how to make old project compatible.
Other than that, I would advise to look at the brand new UI System, which is very powerful: Unity official tutorials - UI
PS: Also, if you're using the old GUITexture component, the image you feed into it, must be marked as "Legacy GUI" I believe.
This is great, except im not fluent in C# so im not exactly sure how i would do this. I dont understand why you wouldnt just call the 2 textures and not the private one.
Texture2D is just an image. Nothing happens to it. GUITexture is a component which renders (displays) a Texture2D in the scene. That is what you've been doing already. You get the component called guiTexture and assign to its texture-property the playNormal texture. You can make those variables public, so that you can actually put int a Texture via the inspector field.
Oh okay that makes sense, I solved it by using the updated UI system you recommended. I just used a button ins$$anonymous$$d of a texture. Thanks!
Your answer
Follow this Question
Related Questions
Wearied Error 1 Answer
Issue with Drawing a GUI texture 0 Answers
GUI Texture error. 1 Answer
Rotate Texture 1 Answer
Problem With GUI Circle Thickness 1 Answer