Hello, I am trying to make a console box with Unet, but I am a bit stuck on the entire syncvar concept.
I am trying to instantiate a GameObject and sync it to all clients, then change the text of that gameobject to what was input from the input field. I can do this all without Unet but I can’t sync it. Here is my recent code for a previous attempt.
Oh well, I didn’t even know that GameObject can be a [SyncVar] anyway, I will try to help you out but I need to made this clear for me first. You got GameObject with text component attached to it and you want it to Instantiate on EVERY client and whoever clicks it first it does something?
Yea as soon as I posted that I realized I was trying to sync a GameObject and that doesn’t work. So I am now thinking of a workaround. I will try to explain a bit better. This is just a console so basically a chat box, but it instantiates a text prefab and then changes the text from what the input field has that way it’s as if the player is typing in the console. My only actual issue is syncing the instantiated text while doing it. I managed to get it to instantiate in the game using networkserver.spawn(), but It just places it in the scene and the transform.setparent doesn’t work with that. So I am stuck now.
I could be completely wrong, but I don’t see any ClientRPC methods. You also don’t have a hook on your SyncVar, so nothing will happen.
When you call the Command, all that code in the Command is run on the server only. In order for you to have that sent to all the clients, you need to have a ClientRPC method. ClientRPCs are essentially the reverse of Commands, they are called by the server and ran on the clients. So essentially, you will be sending a Command to the server (which includes the text you want to display as a parameter of that method), and then the server call a ClientRPC method to send the string to all the clients. Something like this:
The Command you posted earlier would only run on the server, which isn’t what you want. You need to send the string through a Command, and then send it back out to the clients via ClientRPCs. The code I posted might work, but if anything it’s to show you the kind of direction you need to go in I believe.
As for SyncVars, those values are only updated with hook functions. Whenever a SyncVar is changed, its new value gets sent to the server and then the server automatically sends it back out to the clients (at least that’s how I think they work). By themselves, they don’t do anything so you need your hook functions to change what they do. Something like:
[SyncVar (hook = "StringChanged")]
string myString;
StringChanged(string value)
{
//Here you can run anything you want to before the string's value is changed.
myString = value;
}
Again, I believe this is how it all works. At the very least, you could hopefully see what I did and then search around how to use them properly if this isn’t how they work.