Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

blocking send text - chat C# script

Discussion in 'Scripting' started by catafest, May 17, 2018.

  1. catafest

    catafest

    Joined:
    May 3, 2014
    Posts:
    36
    I think is a common error make by users, but I don't know what is wrong :
    I make a GameManager with a default text prefab.
    The script work well with focus on enter and add text when I press the space key.
    After the chatBox.isFocused is focus all text is block and is not add to the text area : textObject.
    The script is this :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class GameManager : MonoBehaviour {
    7.     public int maxMessages = 25;
    8.     public string username;
    9.     public GameObject chatPanel, textObject;
    10.     public InputField chatBox;
    11.  
    12.     public Color playerMessage, info;
    13.  
    14.     [SerializeField]
    15.     List<Message> messageList = new List<Message>();
    16.     // Use this for initialization
    17.     void Start () {
    18.        
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.  
    25.         if (!chatBox.isFocused)
    26.         {
    27.  
    28.             if (Input.GetKeyDown(KeyCode.Space))
    29.             {
    30.                 SendMessageToChat("You peressed the space key !", Message.MessageType.info);
    31.                 Debug.Log("Space");
    32.             }
    33.             if (Input.GetKeyDown(KeyCode.Return))
    34.             {
    35.                 chatBox.ActivateInputField();
    36.             }
    37.         }
    38.         else
    39.         {
    40.                 if (Input.GetKeyDown(KeyCode.Return) && chatBox.text != "")
    41.                 {
    42.                     SendMessageToChat(username + "| " + chatBox.text, Message.MessageType.playerMessage);
    43.                     chatBox.text = "";
    44.                 }
    45.         }
    46.     }
    47.  
    48.     public void SendMessageToChat(string text, Message.MessageType messageType)
    49.     {
    50.         if (messageList.Count >= maxMessages)
    51.         {
    52.             Destroy(messageList[0].textObject.gameObject);
    53.             messageList.Remove(messageList[0]);
    54.         }
    55.         Message newMessage = new Message();
    56.         newMessage.text = text;
    57.         GameObject newText = Instantiate(textObject, chatPanel.transform);
    58.         newMessage.textObject = newText.GetComponent<Text>();
    59.         newMessage.textObject.text = newMessage.text;
    60.         newMessage.textObject.color = MessageTypeColor(messageType);
    61.         messageList.Add(newMessage);
    62. }
    63.  
    64.     Color MessageTypeColor(Message.MessageType messageType) {
    65.         Color color = info;
    66.         switch (messageType) {
    67.             case Message.MessageType.playerMessage:
    68.                 color = playerMessage;
    69.                 break;
    70.  
    71.         }
    72.         return color;
    73.     }
    74. }
    75.  
    76. [System.Serializable]
    77. public class Message
    78. {
    79.     public string text;
    80.     public Text textObject;
    81.     public MessageType messsageType;
    82.     public enum MessageType
    83.     {
    84.         playerMessage,
    85.         info
    86.     }
    87. }
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,715
    Sorry, I didn't understand your comment/question. Could you try rephrasing it?
     
  3. catafest

    catafest

    Joined:
    May 3, 2014
    Posts:
    36
    the problem with this code is from knitting code under Update ...
     
  4. catafest

    catafest

    Joined:
    May 3, 2014
    Posts:
    36
    I solve the issue.