How to display all the previous strings except the current strings from the list?

I would like to create some sort of log to display previous strings for my game, but I still cannot figure out how to properly displaying them. I am using a button to run this method every time player clicks it.

public void AddLog()
{
foreach (string logEvent in DisplayDialogueList)
{
logTextList.text = logText;
logText += logEvent;
logText += "\n\n";
}
}


This method supposed to be displaying all the previous strings that has been displayed before inside DisplayDialogueList but I can't limit the displays only for the previous strings, and it will always display all the strings from the list.

I have tried the .Skip method to detect the current index of the list that are currently being displayed but it does not refresh the list, meaning if I loop to skip the first index from the list, it will always skips the first index even the current index to be displayed is second index and so on.

I also having trouble to skip the last index on the list since C# Unity does not support .SkipLast.

Any help would be appreciated.

EDIT: Sorry for the confusion, this is how I managed to distinguish which is the current string to be displayed, it runs on button click

public void Display()
{
//Checks to see which element from the list has the same index number as the current index
if((index == textIndex) && !(DisplayDialogueList.Count <= index))
{
//Displays the string from the list that has passed the argument above
textList.text = DisplayDialogueList[textIndex];
index +=1;
textIndex = index;
}
}


Everytime the button is clicked, the index number will be increased, so it will matches with the string with the same index from the list and displays it.

UPDATE: Ok I managed to skip the current displayed index and last index (thanks to the answer below) but this happens apparently

The Hello, my name is Mira text is in the first index of the list. How do I make sure that text won't be displayed again after the back button is clicked, which run the AddLog()?

The updated code is below:

if(!(index > DisplayDialogueList.Count))
{
firstIndex = DisplayDialogueList.First();
lastIndex = DisplayDialogueList.Last();
foreach (string logEvent in DisplayDialogueList)
{
if(logEvent != lastIndex && !(firstIndex == currentIndex))
{
logTextList.text = logText;
logText += logEvent;
logText += "\n\n";
}
}
}

• For this you can use two list say previousList and currentList move the dialogue form current to previous. then display only previous. – Aryaman Gupta Sep 4 '18 at 9:29
• How do we distinguish which strings in the list are "current" and which are "previous"? Is just the single last entry in the list "current"? (It looks that way from the rest of your question, but "current strings" is pluralised in the title, suggesting there might be more than one) – DMGregory Sep 4 '18 at 10:05
• Updated my questions :) – Mira Sep 4 '18 at 10:25
• @AryamanGupta I followed your method, but the previous list does not refresh itself and it will displays both updated previous list and before the updatedl previous list. How do I fix it? – Mira Sep 4 '18 at 10:37

You can get the last item in your list by something like this :

lastElement = DisplayDialogueList.get(size - 1) Where size is the list size.

Then in the for each loop you simply add a if statement :

If(logEvent != lastElement) { You log it.. }

• Unity does not recognise .get() method, or am I missing something here? – Mira Sep 4 '18 at 10:26
• @Mira: often, code shared in an answer is pseudo-code, or a rough example of the kind of thing you want to do, not literal syntax to copy-and-paste. Here the corresponding C# syntax would be DisplayDialogueList[DisplayDialogueList.Count - 1] (I sometimes write this as an extension method so I can just call list.Last() for brevity ;) ) – DMGregory Sep 4 '18 at 10:47

Alright, after cramming for the whole night, I finally managed to solved this, I just add the displayed string to temporary list called Logs and just loop the Logs list and displays the added strings inside it before I clear them up, so it won't be any duplicates.

public void AddLog()
{
if(!(index > DisplayDialogueList.Count))
{
//Iterate through the recorded logs
for (int i = 0; i < Logs.Count; i++)
{
var log = Logs[i];
var messageSameAsPrevious = i > 0 && log == Logs.Last();
if(messageSameAsPrevious)
{
continue;
}
logTextList.text = logText;
logText += log;
logText += "\n\n";
}
//Clear the temp list
Logs.Clear();
}
}


Much simpler than my previous methods, and since for loop is much more efficient than foreach loop in terms of performance. Thanks to everyone here who gave me the ideas and insights, much appreciated :)

• "since for loop is much more efficient than foreach loop in terms of performance…" While there can be performance differences between for & foreach, the gap isn't necessarily large, and is not significant when you're iterating over a handful of strings once per frame, so beware of premature optimization in areas like this. Sometimes a foreach is still worthwhile for simplicity/clarity/flexibility/ease of editing. – DMGregory Sep 5 '18 at 8:38