- Home /
problem with read a .txt and save the words in an array
Hi everyone. I'm making a card game and i need read a .txt file to get the hability information of each card but i have a problem when i try save the hability in an array. Somebody can help.PS This is the error (NullReferenceException: Object reference not set to an instance of an object Load_txt.ReadFromFile () (at Assets/Script/Load_txt.cs:31))
using UnityEngine;
using System.Collections;
using System.IO;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
public class Load_txt : MonoBehaviour
{
C_ard[] card=new C_ard[3];
void Update()
{
ReadFromFile();
}
private void ReadFromFile()
{
int cont=0;
StreamReader read=new StreamReader(@"C:\Users\Julio\Desktop\Jogo\Trunfo\Nova Pasta\Super Trunfo - Os Vingadores\A\captaoamerica.txt");
string r=read.ReadLine ();
while (r!=null)
{
char[] charcontrol={':'};
string[] fields=r.Split(charcontrol);
for (int i = 0; i < card.Length; i++)
{
card[0].id=fields[0];
if (card[0].id=="A2")
{
Debug.Log("capitao america");
}
}
r=read.ReadLine();
}
}
public class C_ard
{
public string id, nome;
public int forca, velocidade, habilidade, equipamento, inteligencia;
/*public card(string id, string nome, int forca, int velo, int habl, int equi, int inteli)
{
this.id=id;
this.nome=nome;
this.forca=forca;
this.velocidade=velo;
this.habilidade=habl;
this.equipamento=equi;
this.inteligencia=inteli;
}*/
}
}
Answer by Jeff-Kesselman · May 27, 2014 at 08:38 PM
You allocate the card array, but I do not see you allocating the objects in the array.
When you allocate an array of objects all its entries are null until you set them with object references.
Your answer
![](https://koobas.hobune.stream/wayback/20220613150034im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Read a specific line from .txt file. 3 Answers
Reading a file's memory size 1 Answer
How to make a editable text list using external text file 1 Answer
Splitting textasset into an array and binary search 1 Answer
Problem with TextAsset 0 Answers