So, I’ve been exploring Networking through Unity, it’s not an especially complicated process that I am trying to accomplish, in my opinion. I am simply trying to utilize application users to update and share Timer information between one another. Eventually, I will likely remove the Instantiation process from this area, as I determine duplicate timers and the likes, but for now, I am working on simply creating new timers based upon the input received from fellow users creating timers on their end, and outputing one’s own created timers to fellow users in response. I thought I was getting somewhere, a real touch and go process, but ultimately despite my code being ‘apparently’ error free, syntax wise at least, it seems like there is a gap in my understanding which has caused me to run into an error I don’t understand.
UNetWeaver error: Script NetworkManager uses [SyncVar] ItemName but is not a NetworkBehaviour.
UnityEngine.Debug:LogError(Object)
Unity.UNetWeaver.Log:Error(String) (at C:/buildslave/unity/build/Extensions/Networking/Weaver/Program.cs:20)
Unity.UNetWeaver.MonoBehaviourProcessor:ProcessSyncVars() (at C:/buildslave/unity/build/Extensions/Networking/Weaver/MonoBehaviourProcessor.cs:31)
Unity.UNetWeaver.MonoBehaviourProcessor:Process() (at C:/buildslave/unity/build/Extensions/Networking/Weaver/MonoBehaviourProcessor.cs:18)
Unity.UNetWeaver.Weaver:ProcessMonoBehaviourType(TypeDefinition) (at C:/buildslave/unity/build/Extensions/Networking/Weaver/UNetWeaver.cs:1126)
Unity.UNetWeaver.Weaver:CheckMonoBehaviour(TypeDefinition) (at C:/buildslave/unity/build/Extensions/Networking/Weaver/UNetWeaver.cs:1639)
Unity.UNetWeaver.Weaver:CheckNetworkBehaviour(TypeDefinition) (at C:/buildslave/unity/build/Extensions/Networking/Weaver/UNetWeaver.cs:1650)
Unity.UNetWeaver.Weaver:Weave(String, IEnumerable`1, IAssemblyResolver, String, String, String) (at C:/buildslave/unity/build/Extensions/Networking/Weaver/UNetWeaver.cs:1791)
Unity.UNetWeaver.Weaver:WeaveAssemblies(IEnumerable`1, IEnumerable`1, IAssemblyResolver, String, String, String) (at C:/buildslave/unity/build/Extensions/Networking/Weaver/UNetWeaver.cs:1888)
Unity.UNetWeaver.Program:Process(String, String, String, String[], String[], IAssemblyResolver, Action`1, Action`1) (at C:/buildslave/unity/build/Extensions/Networking/Weaver/Program.cs:34)
UnityEditor.Scripting.ScriptCompilation.EditorCompilationInterface:TickCompilationPipeline(EditorScriptCompilationOptions, BuildTargetGroup, BuildTarget)
Failure generating network code.
UnityEditor.Scripting.ScriptCompilation.EditorCompilationInterface:TickCompilationPipeline(EditorScriptCompilationOptions, BuildTargetGroup, BuildTarget)
Here is the code that I am utilizing, so perhaps someone can explain where the error is at, or what is missing that I don’t yet understand well enough to find.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class NetworkManager : MonoBehaviour {
public bool isAtStartup = true;
NetworkClient myClient;
NetworkTimer nt;
//collects Prefab and Container for NetworkTimer class
public GameObject Timer;
public GameObject Content;
// Update is called once per frame
void Update()
{
//The Server is a Client
//The Client is a Server
//The Cake is a Lie
if (isAtStartup)
{
nt.SetGameObjects(Timer, Content);
SetupServer();
SetupLocalClient();
}
else
{
NetworkServer.Listen(4444);
myClient.RegisterHandler(MsgType.UpdateVars, nt.UpdateTimer);
}
}
// Create a server and listen on a port
public void SetupServer()
{
NetworkServer.Listen(4444);
isAtStartup = false;
}
// Create a local client and connect to the local server
public void SetupLocalClient()
{
myClient = ClientScene.ConnectLocalServer();
myClient.RegisterHandler(MsgType.Connect, OnConnected);
isAtStartup = false;
}
// client function
public void OnConnected(NetworkMessage netMsg)
{
Debug.Log("Connected to server");
}
}
//Requires NetworkBehaviour for SyncVar
class NetworkTimer : NetworkBehaviour
{
//Can't utilize MonoBehaviour as a result
private GameObject Timer;
private GameObject Content;
//Required Variables to Sync, error?
[SyncVar]
public string ItemName;
[SyncVar]
public float CookTime;
[SyncVar]
public float HoldTime;
//Functionally importing GameObject References
public void SetGameObjects(GameObject timer, GameObject content)
{
Timer = timer;
Content = content;
}
//Receives Network Message to UpdateVars with the contained variables in the message?
//How? Or am I lacking a crucial detail here?
public void UpdateTimer(NetworkMessage netMsg = null)
{
GameObject timer = Instantiate(Timer, Content.transform);
timer.GetComponentInChildren<TimerHandler>().Set(CookTime, HoldTime, ItemName);
}
}
//Almost useless, since this is it's only function, but seems significant still.
public class MessageTypes
{
public static short UpdateVars;
}
//Msg contents, wonder if I need to refer back to here,
//and maybe include a function to apply these variables when receiving this message?
public class TimerMessage : MessageBase
{
public string name;
public float hold;
public float cook;
}
//Code Snippets and Tutorials don't make for easily readable code...
//So, here is where I have been sending messages, based on what the user is creating
// while also trying to handle the data route from this place as well, so that regardless of whether
// it is sent or received, it takes the same route to creation.
class GameServer
{
NetworkTimer nt;
public void SendTimer(string productName, float holdTime, float cookTime)
{
TimerMessage msg = new TimerMessage
{
name = productName,
hold = holdTime,
cook = cookTime
};
NetworkServer.SendToAll(MessageTypes.UpdateVars, msg);
nt.ItemName = productName;
nt.HoldTime = holdTime;
nt.CookTime = cookTime;
nt.UpdateTimer();
}
}