• 카테고리

    질문 & 답변
  • 세부 분야

    게임 프로그래밍

  • 해결 여부

    미해결

"IndexOutOfRangeException" 에러가 뜹니다...

19.01.20 16:58 작성 조회수 582

0

안녕하세요 강사님.

좋은강의 잘 듣고있습니다.

dictionary2 강의를 따라하는데 마지막 부분에서 아래 에러메세지가 뜹니다.

IndexOutOfRangeException: Array index is out of range.

TcollertionDic3.Start () (at Assets/TcollertionDic3.cs:22)**

어디가 잘못된건지.... 못찾겠습니다.

제가 올린 코딩 첨부드립니다.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class TcollertionDic3 : MonoBehaviour

{

Dictionary<string, Dictionary<int, StatData>> dataDic =

new Dictionary<string, Dictionary<int, StatData>>();

void Start()

{

TextAsset textAsset = Resources.Load<TextAsset>("Datas");

string[] lines = textAsset.text.Split('\n');

foreach (string line in lines)

{

string[] words = line.Split('\t');

if (!dataDic.ContainsKey(words[0]))

{

dataDic.Add(words[0], new Dictionary<int, StatData>());

}

dataDic[words[0]].Add( <---여기가 22번째줄입니다.

int.Parse(words[1]),

new StatData(int.Parse(words[2]), int.Parse(words[3]))

);

}

Debug.Log(dataDic["마법사"][3].mp);

}

}

public class StatData

{

public int hp, mp;

public StatData(int _hp, int _mp)

{

hp = _hp;

mp = mp;

}

}

제가 코딩한 datas (텍스트파일)입니다.

전사 1 1000 50

전사 2 1200 60

전사 3 1400 70

전사 4 1600 80

전사 5 1800 90

전사 6 2000 100

마법사 1 500 200

마법사 2 550 300

마법사 3 600 400

마법사 4 650 500

마법사 5 700 600

마법사 6 750 700

마법사 7 800 800

마법사 8 850 900

마법사 9 900 1000

마법사 10 950 1100

마법사 11 1000 1200

마법사 12 1050 1300

마법사 13 1100 1400

마법사 14 1150 1500

마법사 15 1200 1600

몇시간째 코딩 확인해봤는데 어디가 잘못된건지 잘 모르겠습니다..

답변 2

·

답변을 작성해보세요.

1

toon88님의 프로필

toon88

2019.05.06

저도 같은 오류를 겪었는데요 

텍스트파일에서 \n단위로 못끊어주더라구요;;

한글때매 그런것같네요

0

천승업님의 프로필

천승업

2019.05.08

전사 마법사 한글대신 영문 알파벳으로 키를 변경해서 실행시켜보면 

 

jj 1 1000 50

jj 2 1200 60

jj 3 1400 70

jj 4 1600 80

jj 5 1800 90

jj 6 2000 100

mm 1 500 200

mm 2 550 300

mm 3 600 400

mm 4 650 500

mm 5 700 600

mm 6 750 700

mm 7 800 800

mm 8 850 900

mm 9 900 1000

mm 10 950 1100

mm 11 1000 1200

mm 12 1050 1300

mm 13 1100 1400

mm 14 1150 1500

mm 15 1200 1600

 

KeyNotFoundException: The given key was not present in the dictionary.

System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) (at <d7ac571ca2d04b2f981d0d886fa067cf>:0)

DataManager.Start () (at Assets/DataManager.cs:29)

 

이런 에러가 뜨네요 이거 왜그런지 아시는분 좀 알려주시면 감사하겠습니다.