'I'm trying to move a cube on the screen according to the data from a csvfile, but it's not working. Can someone help me with this code?
I'm trying to move a cube on the screen according to the data from a csvfile, but it's not working. Can someone help me with this code?
using UnityEngine;
public class LeitorCSV : MonoBehaviour
{
public GameObject cubeTest;
public TextAsset csvFile;
// Update is called once per frame
void Update()
{
ReadCSV();
}
private void ReadCSV()
{
string[] records = csvFile.text.Split('\n');
for (int i = 1; i < records.Length; i++)
{
string[] fields = records[i].Split(',');
cubeTest.transform.Translate(float.Parse(fields[1]),
float.Parse(fields[2]), float.Parse(fields[3]));
}
}
}
Solution 1:[1]
I tried it that way and not work either.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.IO;
using System.Collections.Generic;
public class leitorCSV2 : MonoBehaviour
{
public GameObject cubeTest;
public TextAsset csvFile;
// Update is called once per frame
void Update()
{
readCSV();
}
void readCSV()
{
string[] records = csvFile.text.Split('\n');
for (int i = 1; i < records.Length; i++)
{
string[] fields = records[i].Split(',');
float x = float.TryParse(fields[1], out x) ? x : 0;
float y = float.TryParse(fields[2], out y) ? y : 0;
float z = float.TryParse(fields[3], out z) ? z : 0;
cubeTest.transform.Translate(new Vector3(x, y, z) *
Time.deltaTime);
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | André Anastácio |