- Home /
Change GUITexture Using UnityScript
What's up Unitarians,
I have a small GUITexture acting as a button. Quite simply, it pauses and unpauses the game upon click. The script is put on the GUITexture, which is included into the scene automatically - so its not generated via code.
The problem is that, since the script is on the actual GUITexture, I cannot change the texture of the button programmatically - as to say - whenever I click on it. I want it so that when I click on the button, it does it stuff, and it changes the image of the button.
There are questions similar to this, but in those cases, the script is not actually on the GUITexture, when in this case, it is mandatory that it is.
I'll show you the code as it is:
#pragma strict
public var PauseGUI : GUITexture;
private var paused : boolean = false;
public var Pause_False : Texture2D;
public var Pause_True : Texture2D;
/*function Start () {
}*/
function Update () {
for (var touch : Touch in Input.touches) {
if (touch.phase == TouchPhase.Began) {
// Construct a ray from the current touch coordinates
var ray = Camera.main.ScreenPointToRay (touch.position);
if (Physics.Raycast (ray)) { //What happens when touched
if (paused == false) { //Pause the game
paused = true;
Time.timeScale = 0;
PauseGUI.enabled = true;
this.texture = Pause_True; //This is where it makes no sense
}
if (paused == true) { //Unpause the game
paused = false;
Time.timeScale = 1;
PauseGUI.enabled = false;
this.texture = Pause_False; //This is where it makes no sense
}
}
}
}
}
FYI, PauseGUI is NOT the button itself, it is a completely separate GUITexture that is enabled when the game is "paused"
Answer by robertbu · May 17, 2014 at 03:07 PM
Solution is simple:
guiTexture.texture = Pause_True;
GUITexture is a component. Unity typically when we want to get access to a component, we need to use GetComponnet(), but Unity provides a shortcut for some often used components like GUIText, Transform, and Rigidbody. Note if the component was not one of the ones that had shortcut, you would do:
GetComponent(GUITexture).texture = Pause_True;
Also 'this' refers to your script/component above, not to any other component on this game object.
Your answer
Follow this Question
Related Questions
Please Help!!! my LineRenderer Script is not show when bulid on android device 1 Answer
use HitTest to check if a screen area is touched 1 Answer
Android car controls c# 0 Answers
Disable gui texture in a different way? 0 Answers
Android: How do you position GUITexture and GUIText according to different screen sizes (JS)? 2 Answers