How assign values from a csv file to a array or list

人盡茶涼 提交于 2019-12-25 07:46:14

问题


Currently i'm developing a quiz kind of game. I've managed to develop the basic working of it. I was using the serialized field for updating the questions. Now i'm planning to use a CSV file for the questions.

CSVReader

using UnityEngine;
using System.Collections.Generic;

public class CSVReader : MonoBehaviour
{
public TextAsset csvFile;


[System.Serializable]
public class Row
{

    public string Id;
    public string Fact;
    public string IsTrue;

}




public static List<Row> rowList = new List<Row>();
bool isLoaded = false;

void Start()
{
    Load(csvFile);


}

public bool IsLoaded()
{
    return isLoaded;
}

public  List<Row> GetRowList()
{
    return rowList;
}

public void Load(TextAsset csv)
{
    rowList.Clear();
    string[][] grid = CsvParser2.Parse(csv.text);
    for (int i = 1; i < grid.Length; i++)
    {
        Row row = new Row();
        row.Id = grid[i][0];
        row.Fact = grid[i][1];
        row.IsTrue = grid[i][2];

        rowList.Add(row);
    }
    isLoaded = true;
}

public int NumRows()
{
    return rowList.Count;
}

public static Row GetAt(int i)
{
    if (rowList.Count <= i)
        return null;
    return rowList[i];
}

public static Row Find_Id(string find)
{
    return rowList.Find(x => x.Id == find);
}
public List<Row> FindAll_Id(string find)
{
    return rowList.FindAll(x => x.Id == find);
}
public static Row Find_Fact(string find)
{
    return rowList.Find(x => x.Fact == find);
}
public List<Row> FindAll_Fact(string find)
{
    return rowList.FindAll(x => x.Fact == find);
}
public static Row Find_IsTrue(string find)
{
    return rowList.Find(x => x.IsTrue == find);
}
public List<Row> FindAll_IsTrue(string find)
{
    return rowList.FindAll(x => x.IsTrue == find);
}
}

I'm trying to assign the values to this question class

using UnityEngine;



public class Question : MonoBehaviour
{


CSVReader csvr = new CSVReader();


public string Fact;


public bool IsTrue;



    public void Start()
{
    GameObject PlayCon = GameObject.FindWithTag("GameController");
    if (PlayCon != null)
    {
        csvr = PlayCon.GetComponent<CSVReader>();
    }
    if (csvr == null)
    {
        Debug.Log("Cannot find 'GameController' script");
    }


}

public void FixedUpdate()
{
    for (int i = 1; i <= 2; i++)
    {
        Fact = CSVReader.Find_Id("i").Fact;
        Debug.Log(Fact);
        if (CSVReader.Find_Id("i").IsTrue == "true")
        {
            IsTrue = true;
        }
        else
            IsTrue = false;
    }
} 

}

My game manager for generating questions

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using UnityEngine.UI;


using System.Linq;


using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour {




public Question[] facts;

private static List<Question> unansweredfacts;


private Question currentfacts;


[SerializeField]
private Text FactText;

[SerializeField]
private float TimeBetweenFacts = 1f;


[SerializeField]
private Text TrueAnswerText;


[SerializeField]
private Text FalseAnswerText;


[SerializeField]
private Animator animator;



[SerializeField]
public GameObject canvasquiz;


CSVReader csvr = new CSVReader();






void Start()
{


    if (unansweredfacts == null || unansweredfacts.Count == 0)
    {
        unansweredfacts = facts.ToList<Question>();
    }


    SetCurrentfact();

    Debug.Log(currentfacts.Fact + "is" + currentfacts.IsTrue);




}

void SetCurrentfact()
{
    int RandomFactIndex = Random.Range(0, unansweredfacts.Count);
    currentfacts = unansweredfacts[RandomFactIndex];




        FactText.text = currentfacts.Fact;



    if (currentfacts.IsTrue)
    {
        TrueAnswerText.text = "CORRECT !";
        FalseAnswerText.text = "WRONG !";
    }
    else
    {
        TrueAnswerText.text = "WRONG !";
        FalseAnswerText.text = "CORRECT !";
    }


}

IEnumerator TransitionToNextFact()
{
    unansweredfacts.Remove(currentfacts);
    canvasquiz.SetActive(false); 
    yield return new WaitForSeconds(TimeBetweenFacts);

    SetCurrentfact(); 

    canvasquiz.SetActive(true); 


}



 public void UserSelected(bool isTrue)
 {
    animator.SetTrigger(isTrue.ToString());
    //Debug.Log(isTrue.ToString);
    if (currentfacts.IsTrue == isTrue)
    {
        Debug.Log("CORRECT !");

  }
    else
    {
        Debug.Log("WRONG !");

    }


    StartCoroutine(TransitionToNextFact());
}

Getting a NullReferenceException on SetCurrentFact() every time i try doing this. Even tried assigning the values directly on that method. Being stuck up on this for 2 days. Is there any way i can do this. I know i'm missing something. Sorry for my messed up code.


回答1:


it appears facts field is not initialized before using.

but that would cause the error in Start method instead of SetCurrentfact



来源:https://stackoverflow.com/questions/42594257/how-assign-values-from-a-csv-file-to-a-array-or-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!