- Home /
[Solved]Using an array of two buttons?
Hello, I'm trying to use an array to define textures 2 buttons!something like this:
var Textures : Texture[];
var TextureID : int;
function OnGUI(){
if (GUI.Button (Rect (141, 97,50,50), Texture[TextureID ])){ //TextureID = 1
}
if (GUI.Button (Rect (0, 97,50,50), Texture[TextureID ])){ // TextureID = 2
}
}
Anyone have any idea? Tanks
Answer by robertbu · Nov 24, 2013 at 08:36 PM
TextureID cannot be 1 and 2 at the same time. Also arrays are 0 based, so you can do:
var Textures : Texture[];
function OnGUI(){
if (GUI.Button (Rect (141, 97,50,50), Texture[0])){
}
if (GUI.Button (Rect (0, 97,50,50), Texture[1])){ //
}
}
But I want that when you click a button, change the texture of it! like this: var Textures : Texture[]; var TextureID : int; function OnGUI(){ if (GUI.Button (Rect (141, 97,50,50), Texture[TextureID ])){ TextureID = 1 } if (GUI.Button (Rect (0, 97,50,50), Texture[TextureID ])){ TextureID = 2 } } Just changing the texture of the button clicked
Without more information it is hard to give you an accurate answer. Do you want the texture to cycle, or is it a one-time change? Why are both buttons pulling textures from the same array? For a one time change you can do:
var Textures : Texture[];
var id0 = 0;
var id1 = 2;
function OnGUI(){
if (GUI.Button (Rect (141, 97,50,50), Texture[id0])){
id0 = 1;
}
if (GUI.Button (Rect (0, 97,50,50), Texture[id1])){
id1 = 3;
}
}
It is a bit ugly. I'd likly give each button its own array of textures.
Your answer
![](https://koobas.hobune.stream/wayback/20220613122638im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
DIALOGUE BRANCH CREATION? 1 Answer
Problem making buttons from array of array 1 Answer
Label an array of buttons with an array of strings 2 Answers
Change color of all buttons listed in array 3 Answers
Array out of range! 1 Answer