Question about pulling variable types from a List

I am trying to Implement a dialogue system for my game. I have created a class called TextBox that is 3 strings. The three strings display letter my letter to make it look like talking, yada yada. I have created a generic list of TextBoxes (List.) so that the conversation moves along (like in classic rpgs). I’ll call these conversations.

I have this part set up perfectly and working very well. What I want to do now is have a list of conversations to scroll through each time you talk to the person. So you click on them once, the first conversation will play through with some unspecified number of TextBoxes playing in order. The second time you click a second conversation will play through and so on.

So I thought the best way to go about this was to create a List of my TextBoxes List. This didn’t work out as well as I thought, so I tried to create a new class that I named Conversation. The Conversation class is a generic list of TextBoxes from the TextBox class. So then in the script for the game I created a list of conversations (List.). This worked out well, its easy to define all the TextBoxes for all the Conversations in the inspector like I wanted.

The problem Im running into is that when I try pull the specific TextBoxes out of Conversation Lists (with a for loop) I get a downcast error and it doesn’t work. Im wanting to assign a new var that is a List of TextBoxes (the pieces of the Conversation) from the Conversations.

Is there a way to pull the pieces that make up the first List (TextBoxes) from the over-encompassing List (Conversations) into a list of just TextBoxes?

my code looks like this:

import System.Collections.Generic;

var Conversations : List.<Conversation>;

var selectedConversation = TextBlock;

var selectedTextBoxes = List.<TextBox>;


function Start(){
	
	selectedConversation = Conversations[0];
	
	for( var x in selectedConversation){
		
		selectedTextBoxes.Add(x);
	}
}

I also tried this for the start function:

function Start(){
	
	selectedConversation = Conversations[0];
	
	selectedTextBoxes = selectedConversation;
}

No luck with either. Any help would be appreciated.

1 Answer

1

class Conversation { var aCollection : List.< TextBox >; }

for ( var x : TextBox in selectedConverations.aCollection )

You can’t foreach through just anything - it’s gotta be a collection of some kind which implements the proper interface and such.

Side note - in Unity, only classes and functions should start with capital letters, or you’ll only confuse yourself/your helpers later.