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 :"

14
  • 1
    You realise strData[] is initialised with a single element? What is realData? Looks like you need to step through this to debug. Commented May 30, 2018 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? Commented May 30, 2018 at 17:49
  • As the others have said, you're overwriting realData. Use += instead of just =. Or even better, use a string builder Commented May 30, 2018 at 17:50
  • 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? Commented May 30, 2018 at 17:54
  • 1
    Should i delete this thread or not ? Commented May 30, 2018 at 17:54

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Comments are not for extended discussion; this conversation has been moved to chat.
Hey sir . 1 last question
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.

5 Comments

By the way how can i print it one value per line not only 1 line?
Do what I told you to do in the comments.
@TylerS.Loeper if i don't use a string[] then how can i display them ? i cant use foreach to them . should i use length?
@ChristianTindoc TBH, this should be another question.
@TylerS.Loeper i couldn't write debug.logon my custom class you know that sir right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.