- Home /
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");
}
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.
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");
}
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");
}
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.
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.
@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.
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.
@$$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?
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?
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.
Array is useless in Unity. Use String[] or another IEnumerable of String.
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.
Your answer
Follow this Question
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