Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. 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:
    78
    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,712
    Sorry, I didn't understand your comment/question. Could you try rephrasing it?
     
  3. catafest

    catafest

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

    catafest

    Joined:
    May 3, 2014
    Posts:
    78
    I solve the issue.
     
  5. allen_unity208

    allen_unity208

    Joined:
    Apr 21, 2022
    Posts:
    1
    May I know the solution?