- Home /
Label an array of buttons with an array of strings
Hello,
I'm trying to label an array of buttons using an array of strings, but I get an error message saying: "Ambiguous reference 'Button': UnityEngine.GUI.Button(UnityEngine.Rect, UnityEngine.GUIContent), UnityEngine.GUI.Button(UnityEngine.Rect, UnityEngine.Texture), UnityEngine.GUI.Button(UnityEngine.Rect, String)."
I've looked through some answers here but could not solve my problem. What I've got so far is:
var estCas : Array = new Array[9];
function OnGUI () {
for (var i = 0; i <= 8; i = i++){
if (GUI.Button (Rect (posX, posY, size, size), estCas[i])) {
}
}
}
Funny is that if I concatenate estCas[i] with an empty string it works, well, somewhat, unity crashes a couple seconds later and the buttons won't even appear.
Any help will be realy, realy apreciated!
*Edit (Adding full code)
What I'm trying to do is simple a Tic Tac Toe game as I'm begining with programming.
var cell : Array = new Array[9];
var cellEst : Array = new Array[9];
var turn : boolean; // false = X; true = O
function Start () {
for (var i = 0; i <= 8; i = i + 1){
cellEst[i] = ""; //Gives an empty string that can later be changed to X or O
}
}
function OnGUI () {
for (var i = 0; i <= 8; i = i + 1){
//Arranges the buttons into an tic tac toe board
var posX : int = 0;
var posY : int = 0;
var size : float = 100;
if (i == 1 || i == 4 || i == 7) {
posX = size;
}
if (i == 2 || i == 5 || i == 8) {
posX = size * 2;
}
if (i == 3 || i == 4 || i == 5) {
posY = size;
}
if (i == 6 || i == 7 || i == 8) {
posY = size * 2;
}
if (GUI.Button (Rect (posX, posY, size, size), cellEst[i]) && cellEst[i] == "") {
if (turn == false) {
}
else {
}
turn = !turn; //Changes player
}
}
}
FIXED CODE
var estCas : String = new String[9]; //Don't forget to add a value to the called index
function OnGUI () {
for (var i = 0; i <= 8; i = i + 1){ //Unity crashed when I used i++
if (GUI.Button (Rect (posX, posY, size, size), estCas[i])) {
}
}
}
Thanks a lot to Dave A, Eric5h5 and Owen Reynolds!!!
Answer by DaveA · Sep 24, 2012 at 09:06 PM
I'm betting that estCas is declared like:
var estCas; or var estCas = new Array(); or something like that.
try this:
var estCas : String[];
rsrsrs, nice try, but no. It's more like this:
var estCas : Array = new Array[8];
Sorry, should have added it before! I'll edit it! And thanks a lot for guessing!
Er, no, what Dave A. said is basically what you're doing. Don't use Array. Use built-in arrays, like String[], or generic List.
Answer by Owen-Reynolds · Sep 24, 2012 at 10:26 PM
Your loop should use "i is LESS THAN 8" or else make the array be size 9.
The size 8 array has indexes from 0 to 7 (which is 8 things.) The loop runs `(i=0; i<=8 ...)`, which is 9 things. The ninth button is using past-the-end string `estCas[8]`. In C# that's an array-out-of-range error. Javascript it just makes up a Object value, which confuses Button and gives the odd error you saw.
I see what you mean, just raised my array to 9, as I need 9 buttons, but the error persists.
I'm updating the question with the full code, it might explains a little more.
Your answer
Follow this Question
Related Questions
Array out of range! 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
String.Split problem 1 Answer
C# Incrementing a String Array 1 Answer
Javascript - Calling a random String 2 Answers