Click here to Skip to main content
15,897,291 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello !

Here my code :

C#
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];



    }

}


My problem : at the last line "nv_data" doesn't exist at the current context.
I think I know for why but I don't know how I can fix this problem.
I'm forced to integrate this last line in "void update ()" because it varies each frame.

Could you help me, please ?
Posted

nv_data doesn't exist because it is a variable local to the start method, and you are trying to access it in the update method.

That's like putting your mobile phone in the glove-box of your car, and expecting to find it when you open the glove-box in my car!
Local variables are just that: local to the method they are declare within, and are destroyed on exit from the method. The data that it referenced still exists (probably) however, since it is on the heap.

You may be able to solve this by moving the declaration of nv_data outside the method, to make it a class level variable:
C#
private Data_struct [] nv_data;
void start()
   {

But it sounds like you need to go back a few steps and re-read your course notes from the beginning - you seem to be trying to run before you can walk! :laugh:


"t's exactly this point that I think I'm lost.
When I use a constructor to create an object, I'm creating an instance. But concretely, what is it ? "



The problem is that this is pretty much fundamental: everything in C# is object based, which means that with very few exceptions everythign is instance based.

What is an instance? Ok...try this.

What colour is a car?

Answer: It doesn't have one. A car doesn't have a colour.
"This car" has a colour. "That car" has a colour. Your car; my car; his car; these all have a colour. But "A car" doesn't have a colour because not all cars are the same: some are red, some blue, some silver, ... You need to specify exactly which car you are talking about in order to refer to it Colour - which is just one of it's properties - the same applies to all the other properties such as engine size, number of doors, fuel consumption, fuel type, etc.

And that's important, because "car" is an abstract concept, and "this car", "that car", "your car", "my car", "his car" are examples of that abstract concept. You can drive a car, but you need a specific car in order to get down to the shops!

In computer terms, "car" is a class, and "this car" is an instance of the "car" class:
C#
public class Car
   {
   public Color Color;
   public int EngineSize;
   public Car(Color color, int engineSize)
      {
      Color = color;
      EngineSize = engineSize;
      }
   }
...
   Car thisCar = new Car(Color.Red, 1600);
   Car joesCar = new Car(Color.Blue, 5500);


Seriously, my friend: you need to get a book and follow it from beginning to end. You need to know this stuff, and loads of other bits - which you can't pick up on an ad-hoc basis. If you don't, you will just get more and more confused.
 
Share this answer
 
v2
Comments
Coralie B 6-Mar-15 4:35am    
Thank you for the solution and your explanation.
But this doesn't run ...

I had never had any course of the programming. I am in training, and it's not really my role to use the programming but I need it to continue the project.

To be honest I really struggle to understang the programming...
OriginalGriff 6-Mar-15 4:52am    
Seriously, to do anything practical with any .NET language, there is a huge amount you need to learn. Otherwise, you end up creating something that is badly designed, poorly written, bug ridden, and unmaintainable.

And your code is showing a lot of the signs of "I don't understand what is going on here" development.
I'd strongly suggest you get a course, or a book and work though it - that way you get all the elements introduced in a logical progression. Trying to learn just by "doing it" is a bad idea as you have no idea if what you do is the best way or if there is a simpler, cleaner solution. And there very often is! Without "formal" teaching in the subject, you don't even know what you are missing, let alone why it matters!

In this case "this doesn't run" tells me absolutely nothing about your problem: it's like phoning the garage saying "my car broke" and putting the phone down. The garage is not going to arrive where you are with the right bits to fix it any time soon, as they don;t know who you are, where you are, what car it is, what broke, what parts you might need, what help you want, ... see what I mean?
Coralie B 6-Mar-15 5:17am    
When I have said "this doesn't run" means that your solution doesn't a solution for my problem. It's ever the same : nv_data doesn't exist at the current context.

I see what you mean. If I ask specific questions, you want to help me ?

If it's the case : my question would be :
How can I make an animation with a variable which has been declared before, out of my function ("void update ()" in my case) ?
OriginalGriff 6-Mar-15 5:50am    
Asking "specific questions" isn;t a solution: *you don't know what questions to ask* - and that means you don't understand the answers.

Look at my suggestion: "You may be able to solve this by moving the declaration of nv_data outside the method, to make it a class level variable:"
How much of that do you understand?
What did you do to try it?
Coralie B 6-Mar-15 5:57am    
I put "private Data_struct [] nv_data;" in class Data_struct {}
I have understood that I have to declare this variable not like a local variable. Otherwise, this variable would exist only into the function where it is declared.
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_7 : MonoBehaviour {

    private bool paused; 

    private class Data_struct {

        public String marker_name ; 
        public Vector3 [] positions ; 

    }

    private Data_struct [] nv_data ;
    public GameObject [] body  = new GameObject[55]; 

    string[][] datas; 

    int cpt = 0 ; 
    int i;
    int j;
    int k;
    int frame ;


    // When the script instance is being loaded

    void Awake ()

    {
        Application.targetFrameRate = 25; 
    }


    // Use this for initialization

    void Start() 

    {
        paused = true;
        frame = 0;

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

        using (reader) {

            string line = " "; 
            int lineNumber = 10; 

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

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

            nv_data = new Data_struct[names.Length] ;

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

            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 (j = 1 ; j < names.Length+1 ; j++ )

                nv_data[j-1].positions = new Vector3[cpt]; 

            for (i = 0 ; i < cpt ; ++i ) 
            {
                for (j = 1 ; j < names.Length+1 ; j++ ) 
                {
                    nv_data[j-1].positions[i].x = float.Parse(datas[i][j*3-2]);
                    nv_data[j-1].positions[i].z = -float.Parse(datas[i][j*3-1]);
                    nv_data[j-1].positions[i].y = float.Parse(datas[i][j*3]);
                }
            }
        }
    }

    // Update is called once per frame
    void Update () {

        if (frame < nv_data[0].positions.Length) 

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

        {
            if(body[k] != null) 

            {
                body[k].transform.localPosition = nv_data[k].positions[frame]/1000;
            }

            else
                continue;

        }

        frame++; 

        paused = true; 

    }

}
 
Share this answer
 

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