Okay, here is my problem. I have a button and I want it to change its texture whenever it is click on, and then when it is clicked on again, it will change back to its original texture. I wrote a script which contains no errors at all, but it is not working. I have tried rewriting it several different ways but still nothing happens when I click the button. Here is the script and the two images in case you want to test it:
var normalTexture : Texture2D;
var downTexture : Texture2D;
private var whatTexture : boolean = true;
function OnMouseDown () {
if(whatTexture){
guiTexture.texture = downTexture;
whatTexture = false;
}
if(!whatTexture){
guiTexture.texture = normalTexture;
whatTexture = true;
}
}
Now the problem with this code is that you’ll always reset to true since you immideatly after setting whatTexture to false check if it’s false and in that case set it to true. This is very easily fixed by adding an “else” to the second if-statement, like so:
var normalTexture : Texture2D;
var downTexture : Texture2D;
private var whatTexture : boolean = true;
function OnMouseDown () {
if(whatTexture){
guiTexture.texture = downTexture;
whatTexture = false;
}
else if(!whatTexture){
guiTexture.texture = normalTexture;
whatTexture = true;
}
}
When using if-else only one of the statements can be performed each turn.
You could also remove the “if(!whatTexture)” and keep only “else”, since a bool like whatTexture only can have two states. If it’s not the first state, it must be the second.