- Home /
Updating a UI Text at a set rate using numbers from an array
Hello! So I'm writing a script that will display the altitude of my gameobject as it travels. I'm getting its altitude from a text file that I've put into an array. Here's my code so far:
using UnityEngine;
using UnityEngine.UI;
using System.Text;
using System.IO;
using System.Collections;
using System;
public class PlayerController: MonoBehaviour {
private Vector3 position;
private double[] nums;
private double altDisplay;
public Text altText;
void Start()
{
//Import text file and read all.
string text = System.IO.File.ReadAllText("File.txt");
if (text != null)
{
//Split the string of text into an array containing each number in the file.
string[] strings = text.Split (new[] { " ", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
//Convert each string number to a double.
nums = new double[strings.Length];
for (int i = 0; i < strings.Length; i++)
{
nums [i] = Convert.ToDouble (strings [i]);
}
}
}
void FixedUpdate()
{
//Common variables
int Entries = 6992;
float speed = 1.0f;
//Determine the Y altitude
float yMeters;
for (int i = 3; i < Entries; i += 7)
{
yMeters = Convert.ToSingle(nums [i + 7] - nums [i]);
transform.position += transform.up * yMeters * Time.deltaTime * speed;
}
for (int i = 3; i < 6999; i += 7)
{
altText.text = (nums[i]).ToString () + "m ";
}
}
}
The reason my for loop starts at 3 and increments by 7 is because my text file has 1000 lines that look like this
x1 x2 x3 x4 x5 x6 x7
and the altitude that I need is the x4 number of each line. My problem is that when I click play, the only altitude that is displayed is the very last one. I understand that the for loop is probably running through each one very quickly, so I guess my question is: How do I get the text to update at the same rate that my gameobject is updating?
Your answer
Follow this Question
Related Questions
Is there a way to get Unity to generate the MonoDevelop/VS solution WITHOUT actually opening it? 1 Answer
Linux Unity: Recognize dependent dll files 0 Answers
Installed 5.1, Monodevelop wont open 0 Answers
I use Ubuntu, but Monodevelop Autocomplete is not working. is there some additional Add in ? 1 Answer
Mono is being derpy help 0 Answers