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
1
Question by urfx · Jan 13, 2014 at 02:06 PM · javascriptongui

Ambiguous Label error in custom "for i in Array" GUI

Hi I'm trying to list high scores in an array of strings. But I get this runtime error...

Assets/scripts/scriptScreenLeaders.js(96,26): BCE0004: Ambiguous reference 'Label': UnityEngine.GUI.Label(UnityEngine.Rect, UnityEngine.GUIContent, UnityEngine.GUIStyle), UnityEngine.GUI.Label(UnityEngine.Rect, UnityEngine.Texture, UnityEngine.GUIStyle), UnityEngine.GUI.Label(UnityEngine.Rect, String, UnityEngine.GUIStyle).

Here is the offending code!... Typing the i and or the Scores[i] as a String just leads to other casting issues.

        var yheight : float = y5;
 for (i in Scores) {
         print(Scores[i]);
         yheight = yheight + 45;
         print(yheight);
         GUI.Label(Rect(x5,yheight,200,40),Scores[i], "score_style");

 }
Comment
Add comment · Show 1
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 urfx · Jan 13, 2014 at 01:51 PM 0
Share

here is an answer... but if anyone can point me at some information about the error...

     var yheight : float = y5;
 for (i in Scores) {
         var scored: String = i.ToString();
         //print(i); //worked
         yheight = yheight + 45;
         //print(yheight); //worked
         GUI.Label(Rect(x5,yheight,200,40),scored, "score_style");

 }

so i.ToString() did the trick although I did try a number of variations on this already.

2 Replies

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

Answer by urfx · Jan 13, 2014 at 02:37 PM

final solution looked like this.

     var yheight : float = y5;
     for(var i : int = 0; i < 6; i++){
             var scored: String = Scores[i].ToString();
             yheight = yheight + 30;
             GUI.Label(Rect(x5,yheight,200,40),scored, "score_style");
     }
Comment
Add comment · Show 6 · 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 xortrox · Jan 13, 2014 at 02:51 PM 0
Share

You could also do that directly inside the call to GUI.Label like this:

 var yheight : float = y5;
 for(var i : int = 0; i < 6; i++){
     yheight = yheight + 30;
     GUI.Label(Rect(x5,yheight,200,40),Scores[i].ToString(), "score_style");
 }
avatar image Jessy · Jan 13, 2014 at 02:55 PM 2
Share

Learn why, not what.

avatar image urfx · Jan 13, 2014 at 03:57 PM 0
Share

THAN$$anonymous$$S @Jessy I'm not sure what you mean . I'm using unityscript not c#. Yes I would love to know "why" unityscript needs everything typed and when I did "type" it but I did not append the conversion ToString(). Unity threw another "casting" error which I spent hours googling trying to find out "why". So I agree with your comment.

Thanks everyone for adding more insights.

I keep mistaking unityscript for javascript and am starting to think I should opt for c#. In javascript you don't need to type stuff.

avatar image Hoeloe · Jan 13, 2014 at 08:09 PM 0
Share

I should also point out that, judging from your code earlier, you don't understand how foreach loops work (that's for(i in array) loops). $$anonymous$$y answer here might help you to understand why your original code didn't work.

avatar image urfx · Jan 13, 2014 at 08:29 PM 0
Share

@Hoeloe that's a great answer. I'll vote that up on the original thread. You are right I don't understand how they work in Unityscript "js" I presumed with my javascript head on that I was requesting a string value and not as you put it an index position.

what you say here http://answers.unity3d.com/questions/548266/searching-through-an-array.html

"The foreach loop (in Javascript, this looks like for(var i in Array)) does not use the index of the array as its argument, but the value in the array. "

Your answer summed it up for me perfectly, a remarkable effort and a very enlightening answer.

Show more comments
avatar image
1

Answer by MrCranky · Jan 13, 2014 at 03:28 PM

The two other versions of the function (GUIContent and Texture) are usually not used. I presume that you're really wanting the string version. For that, the parameter to Label needs to be a string or something that can be implicitly converted to a string.

If the type of Scores was a builtin string[] array, the for (i in Scores) loop would make i unambiguously a string. If it is a Javascript-style Array, then it could contain anything, and the compiler won't know which. So I can imagine (I haven't tested), the compiler will complain about the ambiguity and ask you to clarify. If Scores was a builtin array of integers (int[]), then it would know that none of the 3 versions of the functions to call will work, because none of them take an integer.

Either way, the solution is the one you've already found: casting it properly yourself, using your knowledge of what type Scores[i] actually has, to remove the ambiguity, and control the conversion, either by using ToString, or some other formatting function.

Comment
Add comment · Show 6 · 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 urfx · Jan 13, 2014 at 04:04 PM 0
Share

@$$anonymous$$rCranky Scores[] starts life as a playerprefs string (of comma separated strings), its then split into an array or strings. So I can add more scores to it.

An example string value is "10 urfx" , not really clear why Unity can't evaluate data on the fly... is it an underlying performance issue?

Anyhow I've learned something and everyone's answers are appreciated. I was hoping to get these basics out of the way before dealing with JSON where I'm going to have deal with an array of types within an array if you follow me... then what?

avatar image MrCranky · Jan 13, 2014 at 04:22 PM 1
Share

Hmm, it's unclear what the problem is then. $$anonymous$$ight be a misunderstanding on what is being done in the for loop then.

for (score in scores) is a loop where in the first iteration score = scores[0], then score = scores[1], etc. In all three scripting languages, score should be the underlying array type, so from what you say a string, which should be fine to pass to Label. Not sure why it would complain.

NB: that's different from your second loop: for (var i = 0; i < 6; ++i), because i is the index there, and i is an integer, although if Scores is a string[], then Scores[i] should be a string.

If you still need a .ToString() before it'll work, it's most likely Scores[i] is not a string like you think. Can you post the exact definition / declaration of Scores?

avatar image urfx · Jan 13, 2014 at 06:27 PM 1
Share

I'm not too sure either, when I'm not doing Unity I'm doing $$anonymous$$ongoDB, node and old style javascript.

I was using a plug-in script that extends unity's playerprefs to allow you to store different array types in unity. it was quite promising until it came to adding another value to a stored array. I couldn't find a method in the plgu-in for that so I ended up handling it with split, sort and Reverse.

it seems playerprefs is not too dissimilar to localStorage in Javascript similar get and set methods anyhow... and similarly you have to parse strings into data objects in order to then insert, add or push data.

I understand that Unity does not have a "local" database like SQLite... I've been digging around for some alternatives as I do have a much more pressing need for storing JSON data... but thats a different thread for a different day. I'm trying to avoid or at least $$anonymous$$imise contact with "cloud" services i.e. hosting costs. So far I have gathered the best option is to find a thirdparty plug-in to stringify and parse JSON so it can be stored in playerprefs...

....

so here is the exact way I get my Scores...

     if(scriptScene$$anonymous$$anager.Get$$anonymous$$erboard())
     {
     var Scores : Array = scriptScene$$anonymous$$anager.Get$$anonymous$$erboard();
     Debug.Log("SCORES "+Scores);
     }

//which calls this function...

     static function Get$$anonymous$$erboard(){
     
         var $$anonymous$$ers : Array = PlayerPrefsX.GetStringArray("leaderboard");
         var    strlatest$$anonymous$$ers : String = $$anonymous$$ers.ToString();
         var    latest$$anonymous$$ers : Array = strlatest$$anonymous$$ers.Split(","[0]);
         if(latest$$anonymous$$ers)
         {
         return latest$$anonymous$$ers.sort().Reverse();
         }
         else 
         {
         return false;
         }
     }

// you are probably thinking what I'm thinking? ditch PlayerPrefsX its not adding anything and just use playerprefs as is.

avatar image Jessy · Jan 13, 2014 at 07:31 PM 0
Share

Array is useless in Unity. Use String[] or another IEnumerable of String.

avatar image MrCranky · Jan 13, 2014 at 07:33 PM 0
Share

There's certainly code out there to handle JSON <-> string conversion, or at least allow it to be queried in a simple structured way. I have to do the same thing myself at some point soon (storing JSON in PlayerPrefs string values).

That's not your problem here though. I confess I'm more comfortable with C#, where Arrays are always strongly typed (you have to have an array of 'something', not just an array). UnityScript seems to support both those ('builtin' arrays) and Javascript style untyped arrays. You've got the latter, and so I'm not confident saying that it should be able to deduce the type of Scores[i] from that at compile time. So it might be a UnityScript quirk relating to Javascript style arrays; and you should just remember that an explicit cast is needed because the type of the objects isn't known until runtime.

I don't know that there's anything to be gained from PlayerPrefsX, but it's also not the source of your problem here - I think it's from UnityScript itself.

Show more comments

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Event.type check doesnt work inside nested list 0 Answers

yield OnGUI 1 Answer

Variables in GUI not updating/changing? (javascript) 1 Answer

Using Time with OnGUI Help 2 Answers

I need some help on inventory. 0 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