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 as a String just leads to other casting issues.

  • var yheight : float = y5;*
  • for (i in Scores) {*
    _ print(Scores*);_
    _
    yheight = yheight + 45;_
    _
    print(yheight);_
    GUI.Label(Rect(x5,yheight,200,40),Scores, “score_style”);
    _ }*_

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 actually has, to remove the ambiguity, and control the conversion, either by using ToString, or some other formatting function.

final solution looked like this.

	var yheight : float = y5;
	for(var i : int = 0; i < 6; i++){
			var scored: String = Scores*.ToString();*
  •   		yheight = yheight + 30;*
    
  •           GUI.Label(Rect(x5,yheight,200,40),scored, "score_style");*
    
  •   }*