hello i have question on unity RPC chat

my script not work
always my program Unity freezes or turn off window game
i use two scripts Please Help me edit script

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class ChatBoxFunctions : MonoBehaviour
{
    [SerializeField]
    ContentSizeFitter contentSizeFitter;
    [SerializeField]
    Text showHideButtonText;
    [SerializeField]
    Transform messageParentPanel;
    [SerializeField]
    GameObject newMessagePrefab;

    bool isChatShowing = false;
    string message = "";

    void Start()
    {
        ToggleChat();
    }

    public void ToggleChat()
    {
        isChatShowing = !isChatShowing;
        if (isChatShowing)
        {
            contentSizeFitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
            showHideButtonText.text = "Hide Chat";
        }
        else
        {
            contentSizeFitter.verticalFit = ContentSizeFitter.FitMode.MinSize;
            showHideButtonText.text = "Show Chat";
        }
    }
    
    public void SetMessage(string message)
    {
      
        this.message = message;
    }
        
    public void ShowMessage()
    {     
        if (message != "")
        {   
            GameObject clone = (GameObject)Instantiate(newMessagePrefab);
            clone.transform.SetParent(messageParentPanel);
            clone.transform.SetSiblingIndex(messageParentPanel.childCount - 2);
            clone.GetComponent<MessageFunctions>().ShowMessage(message);
        }
    }
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;



public class MessageFunctions : MonoBehaviour {
    public static string Player;

    public float Collor;

    void Update()
    {
        Player = database.user;

    }
    public void White()
    {
        Collor = 1;
    }
    public void Red()
    {
        Collor = 2;
    }
    public void Green()
    {
        Collor = 3;
    }
    public void Yelow()
    {
        Collor = 4;
    }
    public void Blue()
    {
        Collor = 5;
    }
   
    [SerializeField] Text text;
  
    public void ShowMessage(string message) {
        if (Collor == 1)
        {
            text.color = Color.white;
         
            text.text = Player + ":" + message ;

        }

        if (Collor == 2)
        {
            text.color = Color.red;
           
            text.text = Player + ":" + message;
        }
        if (Collor == 3)
        {
            text.color = Color.green;
          
            text.text = Player + ":" + message;
        }
        if (Collor == 4)
        {
            text.color = Color.yellow;
         
            text.text = Player + ":" + message;
        }
        if (Collor == 5)
        {
            text.color = Color.blue;
         
            text.text = Player + ":" + message;
        }
    }


    public void HideMessage() {
        Destroy(gameObject);

    }
  

}

Just to help you a bit, Color is 1 l (unless you’re referring to him: https://upload.wikimedia.org/wikipedia/commons/e/ec/Fernando_collor.jpg
in that case you’re right)
switch case is also useful instead of a bunch of if statemens
and you should set up a simple enum for the colors, because this looks, well, bad
when does it freeze? You could even do a debug, to check, at exactly which line it freezes

Forget the enum - if Collor is a color then make it a color and not a float. Then all of the if statements can be discarded

text.color = Collor;
text.text = Player + ":" + message;
1 Like

Yes, or that

collor not work : send me cleer newMessagePrefab :smile:

Error only networkView message Function Script =
Sending RPC ‘ShowMessage’ failed because the number of supplied parameters doesn’t match the RPC declaration. Expected 1 but got zd parameters.
UnityEngine.NetworkView:RPC(String, RPCMode, Object[ ])
MessageFunctions:ShowMessage(String) (at Assets/3D chat/Chat box/MessageFunctions.cs:23)
ChatBoxFunctions:ShowMessage() (at Assets/3D chat/Chat box/ChatBoxFunctions.cs:54)
UnityEngine.EventSystems.EventSystem:Update()

using UnityEngine;
using System.Collections;
using UnityEngine.UI;



public class MessageFunctions : MonoBehaviour {
    public static string Player;

    public Color Collor;

    void Update()
    {
        Player = database.user;

    }
  
   
    [SerializeField] Text text;
        [RPC]
    public void ShowMessage(string message)
    {
        GetComponent<NetworkView>().RPC("ShowMessage", RPCMode.All);
        text.text = Player + message;


    }


    public void HideMessage() {
        Destroy(gameObject);

    }
  

}

GetComponent().RPC(“ShowMessage”, RPCMode.All , message);

error StackOverflowException

Your calling ShowMessage which calls ShowMessage which calls ShowMessage which calls ShowMessage…

Which calls ShowMessage, which calss ShowMessage, which calls ShowMessage, which calls ShowMessage, and boom, crash