2016-06-27 2 views
0

EDITED --- 내 foreach 루프에서 2D 배열에 값을 추가하는 데 문제가 있습니다. 하리는 루프 앞에 i = 0과 배열을 선언 할 필요가 있다고 지적했고 값의 배정을 거꾸로 잡았다 고 지적했습니다. (고맙습니다). 그러나 최종 강도 배열 및 Debug.Log i 및 콘솔의 값을 추가하려고했습니다 "System.Single []"단일 값 대신 읽습니다. 왜 그런지 아십니까? 감사합니다! 여기 텍스트 파일에서 2D 배열로 값 추가 C#

내 코드입니다 : 요소는 다음 값을 포함하지 않는 경우 먼저 배열을 당신이 coordinateX에 할당하는

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

//[Serializable] 
public class MultiArrayList2 : MonoBehaviour { 

public TextAsset datafile; 
private int i; 
private float[,] coordinates; 
private float[] intensity; 

// Use this for initialization 
void Start() { 

    string[] dataLines = datafile.text.Split ('\n'); 
    string[] lineValues; 
    //print (dataLines.Length); 
    i=0; 

    float[,] coordinates = new float[6853, 3]; 
    float[] intensity = new float[6853]; 
    foreach (string line in dataLines) { 

     lineValues = line.Split (' '); 
     float coordinateX = float.Parse (lineValues [0]); 
     float coordinateY = float.Parse (lineValues [1]); 
     float coordinateZ = float.Parse (lineValues [2]); 
     float intens = float.Parse (lineValues [3]); 

     coordinates [i, 0] = coordinateX; 
     coordinates [i, 1] = coordinateY; 
     coordinates [i, 2] = coordinateZ; 

     intensity [i] = intens; 

     Debug.Log (intensity); 

     i++;   

    } 

} 

}

답변

0

몇 점. 당신은 루프 전에이 변수 initialze, i을 initilaized하지 않은

  1. (foreach)
  2. 는 다음 줄에 coordinates 초기화를 이동합니다.
  3. 주석 처리 된 줄의 주석 처리를 제거해야합니다 (필요한 내용입니다).
  4. Debug.Log은 항상 첫 번째 좌표를 찾고, 나는 각 좌표에 대해 원하는 점을 i으로 변경합니다.

시도해보십시오.

i=0; 
int[,] coordinates = new int[6853, 3]; 

foreach (string line in dataLines) { 

    lineValues = line.Split (' '); 
    int coordinateX = int.Parse (lineValues [0]); 
    int coordinateY = int.Parse (lineValues [1]); 
    int coordinateZ = int.Parse (lineValues [2]); 
    float intensity = float.Parse (lineValues [3]); 

    coordinates [i, 0] = coordinateX; 
    coordinates [i, 1] = coordinateY; 
    coordinates [i, 2] = coordinateZ; 

    Debug.Log(coordinates [i, 0]); 
    i++;   
} 
+0

대단히 감사합니다! 그것은 효과가있다! – jrogers12

+0

도와 줘서 기쁘다. 도움이된다면 답으로 표시하면 문제의 해결책이됩니다. –

0

당신이 배열 요소에 아무것도 지정되지 않습니다, 그것은 다른 방향이어야합니다.

coordinates [i, 0] = coordinateX; 
관련 문제