I’m trying to develop a Brownian movement simulation on Unity. But i’m having some issues with the output data. I do expected that the array “var[ ]” return me just 1000 values on the txt file, but instead, the txt arquive have almost 500,000 lines of data. Can some one pls show me the error on my code
string path = "C:/Users/Documents/test.txt";
public GameObject ParticlesPreFab;
public GameObject ensamble;
public int ParticleEnsanble;
public int tempo = 1000;
int j = 0;
int numbParticles;
float[] media;
float[] var;
public float[,] position;
bool calculate = true;
private void Awake()
{
}
// Start is called before the first frame update
void Start()
{
for (int i = 0; i < ParticleEnsanble; i++)
{
GameObject go = Instantiate(ParticlesPreFab, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
go.transform.parent = GameObject.Find("Ensamble").transform;
}
numbParticles = ensamble.gameObject.transform.childCount;
position = new float[tempo, numbParticles];
var = new float[tempo];
media = new float[tempo];
}
// Update is called once per frame
void FixedUpdate()
{
if (j < tempo)
{
for (int i = 0; i < numbParticles; i++)
{
float position_x = ensamble.transform.GetChild(i).GetComponent<Rigidbody>().velocity.x;
position[j, i] = position_x;
//Debug.Log(position[j, i]);
}
}
else
{
if (calculate == true)
{
Destroy(ensamble);
calculate = false;
for (int t = 1; t < tempo; t++)
{
media[t] = 0.0f;
for (int ci = 1; ci < numbParticles; ci++)
{
media[t] += position[t, ci] / numbParticles;
}
}
for (int t = 1; t < tempo; t++)
{
var[t] = 0.0f;
for (int ci = 1; ci < numbParticles; ci++)
{
var[t] += Mathf.Pow(position[t, ci] - media[t], 2) / numbParticles;
StreamWriter writer = new StreamWriter(path, true);
writer.WriteLine(var[t]);
writer.Close();
}
}
}
}
j++;
}