Click here to Skip to main content
16,012,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello !

Here my code :

C#
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;
using UnityEngine;
using System;


public class Recup_donnees_6bis : MonoBehaviour {

    class Data_struct {

        public String marker_name ; 
        public Vector3 [] positions ; 

    }

        GameObject [] body ; 
        string[][] datas; 

        int cpt = 0 ; 

    void Awake ()

    {
        Application.targetFrameRate = 25; 
    }

    void Start() 

    {
        body = new GameObject[55]; 

        body [0] = GameObject.Find ("RightUpLeg"); 
        body [1] = GameObject.Find ("LeftUpLed");
        body [5] = GameObject.Find ("Neck");
        body [6] = GameObject.Find ("RightArm");
        body [7] = GameObject.Find ("LeftArm");
        body [8] = GameObject.Find ("Throat");
        body [9] = GameObject.Find ("Spine1");
        body [11] = GameObject.Find ("Spine");
        body [12] = GameObject.Find ("Hips");
        body [13] = GameObject.Find ("Scalp");
        body [14] = GameObject.Find ("HeadTop_End");
        body [15] = GameObject.Find ("R_Temple");
        body [16] = GameObject.Find ("L_Temple");
        body [17] = GameObject.Find ("RightForeArm");
        body [21] = GameObject.Find ("RightHand");
        body [23] = GameObject.Find ("RightHandIndex1");
        body [24] = GameObject.Find ("RightHandThumb2");
        body [25] = GameObject.Find ("RightHandPinky2");
        body [26] = GameObject.Find ("LeftForeArm");
        body [30] = GameObject.Find ("LeftHand");
        body [32] = GameObject.Find ("LeftHandIndex1");
        body [33] = GameObject.Find ("LeftHandThumb2");
        body [34] = GameObject.Find ("LeftHandPinky2");
        body [35] = GameObject.Find ("RightLed");
        body [38] = GameObject.Find ("RightFoot");
        body [41] = GameObject.Find ("RightFootToeBase_End");
        body [42] = GameObject.Find ("RightToeBase");
        body [45] = GameObject.Find ("LeftLed");
        body [48] = GameObject.Find ("LeftFoot");
        body [51] = GameObject.Find ("LeftFootToeBase_End");
        body [53] = GameObject.Find ("LeftToeBase");

        StreamReader reader = new StreamReader ("Suj01_PI_DP_C00_1.txt"); 

        using (reader) { 

            string line = " "; 
            int lineNumber = 10; 

                for (int i = 0; i < lineNumber; i++)
                {
                    line = reader.ReadLine(); 
                    if (line == null) return; 
                }

            string line_10;
            line_10 = line; 
            string[] names = line_10.Split (new String[] {",",",,,"},StringSplitOptions.RemoveEmptyEntries);

            Data_struct [] nv_data ;
            nv_data = new Data_struct[names.Length] ;

                for (int i =0 ; i< names.Length; ++i )
                {
                    nv_data[i] = new Data_struct () ; 
                    nv_data[i].marker_name = names[i] ; 
                }

            line = reader.ReadLine(); 
            string line_11;
            line_11 = line;
            string[] axes = line_11.Split(new String[] {"Field #",","},StringSplitOptions.RemoveEmptyEntries); 
           

            datas = new string[4000][]; 

            int counter = 0;
            while (line != null) { 
                counter++;
                line = reader.ReadLine(); 

                if (line == "ANALOG")
                break ;

                if ((counter %3) != 1) 
                    continue;

                string lines_datas; 
                lines_datas = line;
                datas[cpt] = lines_datas.Split(new string[] {","},StringSplitOptions.RemoveEmptyEntries); 

                line = reader.ReadLine(); 


                cpt ++ ;

 
            }

                for (int i = 0 ; i < cpt ; ++i ) 
                {
                    for (int j = 1 ; j < names.Length+1 ; j++ ) 
                {
                    nv_data[j-1].positions = new Vector3[cpt]; 

                nv_data[j-1].positions[i].x = float.Parse(datas[i][j*3-2]);
                nv_data[j-1].positions[i].y = float.Parse(datas[i][j*3-1]);
                nv_data[j-1].positions[i].z = float.Parse(datas[i][j*3]);
                }

            }

        }

    }

 void update ()

{

        // To associate names_markers to avatar_joints :

        for (int k = 0 ; k < body.Length ; ++k)

            body[k].transform.position = nv_data[k].positions[k];



    }

}



The problem : (at the list line of the code)
nv_data[k].positions[k]

is in red

I think it's because my code's lines at the end, is not in "using (reader)" where I've saved nv_data. I am forced to create nv_data at this place, otherwise, how can I do ?


Thank you for your help
Posted
Updated 5-Mar-15 3:43am
v3
Comments
CHill60 5-Mar-15 9:13am    
If you click on the Error list tab what is the error that is listed? Alternatively if you hover your mouse over the red bit it will also tell you want the error is
Coralie B 5-Mar-15 9:15am    
It's wrote : The name 'k' does not exist in the current context
DamithSL 5-Mar-15 9:19am    
is this our full code of update method or customized version for the question?
Coralie B 5-Mar-15 9:23am    
I have just seen my error...
I have integrated "void update" in "using (reader)" and it's not possible.

I'm going to update my question ...

if you not specify the for loop scope by using {} then it will consider only the next line.
if your variable k is in another line below the very next line to for loop then you will get error
example:
C#
for(int i=0; i<5; i++)
   Console.WriteLine("My first Line"); // below line give you error
   Console.WriteLine(i);

// use {} and specify the scope for the loop as below
for(int i=0; i<5; i++)
{
   Console.WriteLine("My first Line");
   Console.WriteLine(i);
 }

update:
update method need () like below
C#
void update ()
{
  //your code
}

and declare nv_data in the same place where you declare body
C#
Data_struct[] nv_data;
GameObject[] body;
string[][] datas;
 
Share this answer
 
v3
Comments
Coralie B 5-Mar-15 9:34am    
Thank you for your solution.

But I have discovered it's not that the problem actually.

And I assume that we don't need {} if we have only 1 line which depends of this loop. Is it not exact ?
Coralie B 5-Mar-15 9:35am    
I've just updated my question.

Have you seen my code ?
DamithSL 5-Mar-15 9:39am    
yes, check my updated answer
Coralie B 5-Mar-15 9:43am    
Sorry, again.

In my code it has () already. But when I have copied my code, I have erased "void update ()" sans faire exprès (sorry I'm french and I don't know how we can say this expression).

Actually, my problem is nv_data doesn't exist out "using (reader)". And I don't know how I can fix this problem.
DamithSL 5-Mar-15 9:53am    
you can declare nav_data in the same place where you define body. otherwise nav_data can't access from other method. read the msdn documentation for variable/method scope
Variable scope, in order for your objects body[] and nv_data[] to be seen outside the scope of the method "Start" you either have to elevate the variable scope outside the "Start" method or you need to pass body[] and nv_data[] to the "update" method.

Cheers
 
Share this answer
 
Comments
Coralie B 5-Mar-15 10:03am    
Sorry, but I'm not english and I don't understand what you want to mean.
Can you explain in a different way please ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900