- Home /
How will we enable and disable a gui texture in unity 3d?
Hi Every one,
I have a small task. That is very small task but there is i don't know the error where it is. By default my texture is on position. I want that into disable.
i wrote code like this:
var someTexture : Texture2D;
function Update () {
if (Input.GetKeyDown ("space")){
if (someTexture.enabled == true){
someTexture.enabled = false;
}
else {
someTexture.enabled = true;
}
}
}
My problem here is, I attached this script to main camera and added texture in the inspector of the main camera. After then play mode. Nothing will be getting in the game scene. what can i do? i want by default that texture is in show in game scene and if i press space button that will disable .and again i press space button i want to display.
please help me. thankyou
Answer by yusolaifdfer · Apr 01, 2013 at 10:45 AM
You can declare the someTexture as a GameObject, and then use
someTexture.active = false;
or
someTexture.SetActive(false);
since GameObject.SetActive is obsolete now.
Are u telling this thing. Take one empty gui texture. in that add a texture in to inspector. Then renamed that texture in the hierarchy view. and after write code above and i attached that into main camera. then drag that texture object in the main camera inspector. but that is not working.
var someTexture : GUITexture;
function Update () {
if (Input.Get$$anonymous$$eyDown ("space")){
if (someTexture.enabled == true){
someTexture.enabled = false;
}
else {
someTexture.enabled = true;
}
}
}
Answer by hexagonius · Apr 01, 2013 at 10:49 AM
The part where you actually place it is missing. You could try two different approaches:
Use the OnGUI() method to draw it into your viewport:
or transform the texture in your scene by using one of these two methods:
The first approach is easier to understand.
GUITexture is a part of GUILayer, so OnGUI doesn't handle it.
Answer by mohammadkhatri · Apr 01, 2013 at 01:03 PM
Here Is The Problem.. In place of Texture2D U need to put GUITexture
var someTexture : GUITexture
Answer by Ngo · Sep 19, 2013 at 07:26 AM
I have same problem, and I discover this way is also fine
var showHPBar:GUITexture;
var MaxHP:int = 1000;
var HP:int = 500;
function Update(){
showHP();
}
function showHP(){
if(MaxHP != HP){
showHPBar.gameObject.SetActive(true);
}
}
Answer by united4life · Sep 19, 2013 at 08:13 AM
This is a tested project written in C# and working fine.
public GUITexture texture;
void Update () {
if(Input.GetKeyDown(KeyCode.Space))
{
if(texture.enabled == true)
texture.enabled = false;
else
texture.enabled = true;
}
}