Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# UNET-对象通过网络同步,但文本组件为';T_C#_Unity3d - Fatal编程技术网

C# UNET-对象通过网络同步,但文本组件为';T

C# UNET-对象通过网络同步,但文本组件为';T,c#,unity3d,C#,Unity3d,大家好,我正在尝试为一个多人游戏制作聊天,我已经在所有客户端和主机上实例化了消息预置,所以效果很好。唯一的问题是文本没有改变,它保持为默认值。以下是我的代码: ChatManager.cs: using System.Collections.Generic; using UnityEngine; using System.Collections; using UnityEngine.Networking; public class ChatManager : NetworkBehaviour

大家好,我正在尝试为一个多人游戏制作聊天,我已经在所有客户端和主机上实例化了消息预置,所以效果很好。唯一的问题是文本没有改变,它保持为默认值。以下是我的代码:

ChatManager.cs:

using System.Collections.Generic;
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class ChatManager : NetworkBehaviour
{
    [SerializeField] private GameObject chat;
    [SerializeField] private GameObject chatItemPrefab;
    [HideInInspector] public List<ChatItem> messages;

    public float messageExpireTime = 5;

    public static ChatManager instance;

    void Awake()
    {
        instance = this;
    }

    public void ChatSendMessage(string sender, string message)
    {
        CmdChatSendMessage(sender, message);
    }

    [Command]
    void CmdChatSendMessage(string sender, string message)
    {
        GameObject chatItem = Instantiate(chatItemPrefab);
        chatItem.transform.SetParent(chat.transform);
        ChatItem messageSettings = chatItem.GetComponent<ChatItem>();
        messageSettings.Setup(sender, message);
        messageSettings.chatNetId = chat.GetComponent<NetworkIdentity>().netId;
        NetworkServer.Spawn(chatItem);
        StartCoroutine(WaitForExpire(chatItem));
    }

    public IEnumerator WaitForExpire(GameObject chatItem)
    {
        yield return new WaitForSeconds(messageExpireTime);
        CmdDestroyChatMessage(chatItem);
    }

    [Command]
    void CmdDestroyChatMessage(GameObject chatItem)
    {
        NetworkServer.Destroy(chatItem);
    }
}
如果使用主机上的参数“Server”和“hello!”调用“ChatSendMessage”:

主持人看到:

但客户看到:

这是默认文本


我正在处理的问题是什么。有什么想法吗?谢谢

修复-我也在start方法中设置了文本,因此如果您是发送人,它会被安装方法覆盖,但是如果您是远程客户端,安装程序从未运行过,您可以从syncvar中设置文本,该文本已从发送人的客户端的安装方法中设置。e、 g

void Start()
{
    GameObject chatParent = ClientScene.FindLocalObject(chatNetId);
    transform.SetParent(chatParent.transform);
    senderText.text = string.Format("[{0}]:", sender);
    messageText.text = message;
}
void Start()
{
    GameObject chatParent = ClientScene.FindLocalObject(chatNetId);
    transform.SetParent(chatParent.transform);
    senderText.text = string.Format("[{0}]:", sender);
    messageText.text = message;
}