- Home /
List of string is duplicated [C#]
Code:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class Manager : MonoBehaviour
{
bool isChatting;
bool sendText;
public Text chatText;
List<string> chatMsg;
private string display = "";
string playerName;
public GameObject menuPanel;
public GameObject chatPanel;
public GameObject optionPanel;
public GameObject multiplayerPanel;
public GameObject joinPanel;
public GameObject createServerPanel;
public GameObject pausePanel;
void Awake()
{
chatPanel.SetActive(false);
}
void Start()
{
playerName = "SuperHB";
chatMsg = new List<string>();
}
void Update()
{
if(Input.GetKeyDown(KeyCode.Y) && isChatting == false)
{
isChatting = true;
chatPanel.SetActive(true);
sendText = true;
}
if(Input.GetKeyDown(KeyCode.Escape) && isChatting == true)
{
isChatting = false;
chatPanel.SetActive(false);
sendText = false;
}
if(Input.GetKeyDown(KeyCode.Return) && sendText == true)
{
OnSend();
}
}
public void Msg(string writtenMsg)
{
chatMsg.Add(writtenMsg.ToString());
}
public void OnSend()
{
foreach(string msg in chatMsg)
{
display = display.ToString() + playerName.ToString() + ": " + msg.ToString() + "\n";
}
chatText.text = display;
}
}
Hierarchy Close-up:
Comment
are you clearing the string you send to $$anonymous$$sg()
after you add it to the chat log? the call that method isn't in your post so hard to tell...
Best Answer
Answer by rob5300 · Jan 30, 2015 at 06:33 PM
This is happening as you are using an array to store the newly sent messages, and adding the list to the chat box each time. Instead you should store it in a string, and just add this new string to the chatText.text. After this you can add the string to the array.
Example:
chatText.text += "\n" + newMessageThatWasSent;
//Then if we want a record of the messages in an array we add this new message to it.
chatMessagesLog.Add(newMessageThatWasSent);
Also you don't need to use ToString() on a string.
Your answer
