Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Rodrigo M Porto · Sep 24, 2012 at 07:43 PM · arraystringbuttonslabel

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!!!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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[];

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rodrigo M Porto · Sep 24, 2012 at 09:49 PM 0
Share

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!

avatar image Eric5h5 · Sep 24, 2012 at 11:37 PM 0
Share

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.

avatar image Rodrigo M Porto · Sep 24, 2012 at 11:49 PM 0
Share

Woah!!! Worked as a charm! Thanks a million!!!

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rodrigo M Porto · Sep 24, 2012 at 11:33 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges