Using an array as argument for string.Format()
Quick fix.
var place = new object[] { 1, 2, 3, 4 };
C# does not support co-variant array conversion from int[]
to object[]
therefor whole array is considered as object
, hence this overload with a single parameter is called.
It is possible to pass an explicit array for a params
argument, but it has to have the matching type. string.Format
has a few overloads, of which the following two are interesting to us:
string.Format(string, params object[])
string.Format(string, object)
In your case treating the int[]
as object
is the only conversion that works, since an int[]
cannot be implicitly (or explicitly) converted to object[]
, so string.Format
sees four placeholders, but only a single argument. You'd have to declare your array of the correct type
var place = new object[] {1,2,3,4};
You can convert int array to string array as pass it using System.Linq
Select()
extension method.
infoText.text = string.Format("Player1: {0} \nPlayer2: {1} \nPlayer3: {2} \nPlayer4: {3}",
place.Select(x => x.ToString()).ToArray());
Edit:
In C# 6 and above, you can also able to use String Interpolation
instead of using string.Format()
infoText.text = $"Player1: {place[0]}\nPlayer2: {place[1]} \nPlayer3: {place[2]} \nPlayer4: {place[3]}";
Check this fiddle for your reference.