- Home /
Changed array type, different functions?
Hi Forum!
EDIT: I have changed approach to how I create arrays, for my server, and apparently this given me severe problems. I now use the layout of the template chatroom by Unity, with this approach:
class Server_Clientlist_Entry
{
var text = "";
var Style = 1;
}
function Server_Clientlist_Add (str : String, Style : int)
{
var entry = new Server_Clientlist_Entry();
entry.text = str;
Debug.Log(entry);
if (Style == 1) entry.Style = 1;
if (Style == 2) entry.Style = 2;
if (Style == 3) entry.Style = 3;
if (Style == 4) entry.Style = 4;
if (Style == 5) entry.Style = 5;
Server_Clientlist_Entries.Add(entry);
Server_Clientlist_ScrollPosition.y = 1000000;
}
This gives me a problem. For some reason, when I try to add lines with this, it prints them right onto the screen, but it stores the name of the class "Server_Clientlist_Entry" in the array, and not the actual string, so I can't e.g. print or search compare to any index, since all index values are apparently "Server_Clientlist_Entry" .. Maybe this is since it uses a class, but how do I avoid this? I want to be able to loop through the index, and check if an entry with the same name already exist. This is an edited thread as I learned more from debugging.
Thanks in advance for your time, Jeanette
Okay, after various document lookup-reading I narrowed the error. When I try to use this approach, for some reason it prints the correct input into the chatlines on the screen, after having debugged the variable "entry" it appears that what happens is it doesn't actually store the text I pump into the chat in the array, but just stores the name "Server_Clientlist_Entry" in each line of the array, why is that, what is wrong with my code above. I mean, it prints the right thing out, it it due to the fact that it also uses a class?
class Server_Clientlist_Entry
{
var text = "";
var Style = 1;
}
What is Server_Clientlist_Entries this a list of? Can you post the definition?
It is just defined as a variable, with the actual array.
private var Server_Clientlist_Entries = Array();
Not sure how that works, isn't list a strict C#? This is coded entirely in Java. Not sure how this would make me able to lookup individual strings of the index. As I see it, the problem isn't looking them up, since they ARE there, but it doesn't actually store the right thing in them, in the first place.
Answer by Bunny83 · Dec 26, 2012 at 11:40 PM
First like Mike said you should use a List instead of an Array. The Array class does the same thing but with untyped content. A generic List is strong typed and therefore is faster and type safe.
I guess your actual problem is the place where you access / display your items. I'm pretty sure you have something like that:
GUI.Label(..., Server_Clientlist_Entries[some index]);
But you have to access the string like this:
GUI.Label(..., Server_Clientlist_Entries[some index].text);
You just use the class reference like a string so it returns the class name. You actually want the string that is stored in the text variable of that class.
Your answer
![](https://koobas.hobune.stream/wayback/20220613085944im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Array index out of range? 3 Answers
Compare Contents of arrays 2 Answers
How do I find a String in an Array? 2 Answers
Take only one action ~ if(A = var[i]) reveals a true 1 Answer
Calling System.Net.Dns.GetHostEntry(sHostName) failed 0 Answers