Text UI and Array
Hallo, i try to display the number on the UI.
public float count;
count = 3;
SetCountText();
void SetCountText()
{
countText.text="Count: "+ count;
and in the text its display 3 thats fine.
but i need to have an array so i try it with an array
public float [] count;
count[0] = 3;
SetCountText();
void SetCountText()
{
countText.text="Count: "+ count[0];
and notthing happend, on the display they are written Text and not the number.
i try it with public float [] count = new float[6]; and it doesnt work.
can someone help me :) ?
Answer by do2 · Sep 29, 2015 at 08:17 PM
Try this and post what you find out.
void SetCountText()
{
Debug.Log(count[0]+""); // to ensure the variable has a value
int C = count[0];
countText.text="Count: "+ C;
this is just to test where the error is, you should be able to pass a value inside of an array with no problem.
Answer by Gildart · Sep 29, 2015 at 08:49 PM
Hallo Thanks for your answer, i try it but it doesnt help.
i get in the Error IndexOutOfRangeException: Array index is out of range.
public class PlayerController : MonoBehaviour {
public Text countText;
public float [] count;
void Start () {
count[0] = 3;
SetCountText();
}
void Update ()
{
SetCountText();
}
void SetCountText()
{ Debug.Log(count[0]+"");
float C = count [0];
countText.text="Count: "+ C;
}
}
i have also try it with int [] count; int C= count[0]; and it doesnt work.
mhmmmm :/
Answer by do2 · Sep 29, 2015 at 08:59 PM
Ok Mate. if you are having an IndexOutOfRangeException, then it means that you are not initializing the array correctly.
instead of
count[0] = 3;
do
count = new float[3];
check out the following link for the correct way to initialize and manipulate arrays in c# https://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx
specially this part
int[] numbers; // declare numbers as an int array of any size
numbers = new int[10]; // numbers is a 10-element array
numbers = new int[20]; // now it's a 20-element array