0

Here's my code so far

CUSTOMCLASS.CS

public string[,] tableBR;

string[] strData = {"P  ,B  ,B  ,P  ,B  ,B  ,B  ,B  ,B  ,B  ,P  ,P  ,B  ,P  "};

public int X_LENGTH = 104;
public int Y_LENGTH = 15;

#region BR VARIABLES

string BigRD= "";
string[] newBigRD;
string realData= "";

#endregion
public Scoreboard(){
    tableBR= new string[X_LENGTH,Y_LENGTH   ];
}

public void MakeBR(string data){
    BigRD = data;

    for(int i = 0; i < strData.Length; i++){
        BigRD += strData [i];
        BigRD += ",";
    }

    newBigRD= BigRD .Split (',');

    foreach(string newData in newBigRD){
        realData = newData;
    }
}

public string ShowBigRD(){
    return realData;
}

public override string ToString(){
    return "this are all the data :" + realData.ToString();
}

And here is my main class

MAINCLASS.CS

string BigRD= "";

void Start(){
    StartCoroutine ("Win_Log");
}

IEnumerator Win_Log(){
    Scoreboard scoreBoard = new Scoreboard();

    scoreBoard.MakeBR(BigRD);
    Debug.Log ("This is the data : " + scoreBoard.ShowBigRD());

    yield return new WaitForEndOfFrame ();
}

It gives me a null string value . It only prints

"this are all the data :"

  • 1
    You realise `strData[]` is initialised with a single element? What is `realData`? Looks like you need to step through this to debug. – Alex K. May 30 '18 at 17:45
  • realData is overwritten in every iteration of the foreach loop. You also haven't shown us what realData is. What is its type? – Tyler S. Loeper May 30 '18 at 17:49
  • As the others have said, you're overwriting `realData`. Use `+=` instead of just `=`. Or even better, use a string builder – Daxtron2 May 30 '18 at 17:50
  • @AlexK. as far as what I understand sir I pass the value of `BigRD` to `newBigRD` which is I split and i want to display all of what `newBigRd` have so i uses a `for loop` statement where in `newData` is equal to `realData` so that I can return it and get it from my mainclass –  May 30 '18 at 17:51
  • Sorry for the confusion . I incorrectly put the string . I edited it already –  May 30 '18 at 17:52
  • Put the debug.log inside your for each loop, and just run MakeBr. Put the debug after realdata, and use this instead: Debug.Log ("This is the data : " + realdata); – Tyler S. Loeper May 30 '18 at 17:53
  • @TJWolschon What the heck . Sorry about that . I should have used += –  May 30 '18 at 17:54
  • 1
    Why do you think that's a `null`. It's probably an empty string since you add a comma to the end of `BigRD` so the last item in the loop is going to be an empty string and thus the final value set to `realData`. The real question is what do you want it to print? – juharr May 30 '18 at 17:54
  • 1
    Should i delete this thread or not ? –  May 30 '18 at 17:54
  • Probably, it's a simple typo so it's unlikely to be useful to future readers. Glad we spotted it though :) – Daxtron2 May 30 '18 at 17:55
  • That's not the only problem with his code though. The foreach is not printing each value. But OP can probably figure that out now. – Tyler S. Loeper May 30 '18 at 17:55
  • @TylerS.Loeper It is actually printing all the value –  May 30 '18 at 17:57
  • @TylerS.Loeper b/c it was overwriting them. – Daxtron2 May 30 '18 at 18:00
  • OP wants to print out all the values in one line, or one value per line? I guess it doesn't matter, they solved their problem. Still I don't understand why they use a string[] in the first place if they were just changing it back into a single string. – Tyler S. Loeper May 30 '18 at 18:04

2 Answers2

2

Simple as changing

realData = newData;

to

realData += newData;

Otherwise, you're overwriting your string realData each time through the loop, instead of just appending it.

Daxtron2
  • 1,239
  • 1
  • 11
  • 19
2

I am guessing that you are looking for this:

IEnumerator Win_Log()
{    
    Scoreboard scoreBoard = new Scoreboard();   
    scoreBoard.MakeBigRoad (BigRD);    

    for(int i=0; i< scoreBoard.newBigRD.Length;i++)
    {
        var realData = scoreBoard.newBigRD[i];
        Debug.Log ("This is the data : " + realdata); 
    }

    yield return new WaitForEndOfFrame ();
}

If you decide to do this, you have to make the string[], string[] newBigRD; public by changing it to public string[] newBigRD;, so it can be accessed outside your class.

You may want to fix you string array declaration too. It should look like this,

string[] strData = {"P" ,"B ","B","P","B","B","B","B","B","B","P","P","B","P"};

or else we can keep the split method you are using and use

newBigRD[]:

Which is what I used in my example.

Options for initializing a string array

Tyler S. Loeper
  • 816
  • 11
  • 22
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/172106/discussion-on-answer-by-tyler-s-loeper-cannot-print-the-foreach-value-from-the). – Brad Larson May 30 '18 at 18:51
  • Hey sir . 1 last question –  May 30 '18 at 19:54