0
\$\begingroup\$

I've added some text to a unity UI and set both the achor and pivot to the top right corner as shown here:

enter image description here

When I start this in debug in unity the text looks fine:

enter image description here

What's weird is that when I build the app and run it as an executable as a host (this is a multiplayer networked game) the text also looks fine. But when I start a client instance the text doesn't appear.

I debugged and paused the game in unity in client mode and noticed that the text object is miles away from where I'm expecting it to be (and as a result doesn't show on the camera).

You can just about see the black line on the below image which is my level and the text anchored miles up and right of the level.

It seems like I've done something stupid here but I can't work out what :s

enter image description here

EDIT: Leo's answer was correct conceptually, but here's how I implemented the suggestion.

  1. Created an empty game object GameStateManager in the hierarchy
  2. Add a GameState script to the manager with a network identity
  3. Added my 'RemainingTime' property to the game state script and put the [SyncVar] attribute on it
  4. Put logic in the game state Update method to update the remaining time
  5. Added a ClientTimer script to the text object (mono behaviour), which retrieved the GameState from within the clients copy of the game state manager.
  6. In the update method of the client time, get a reference to the Text object and set the .text property to the value of the remaining time.

I'm not sure if this is correct, but it does seem to work, and now the ClientTimer script isn't networked, which resolved the issues with placement of the text.

\$\endgroup\$
  • \$\begingroup\$ Are you rendering mode are you using in the canvas? \$\endgroup\$ – Leo Sep 14 '16 at 20:24
  • \$\begingroup\$ The canvas is using screen space overlay. I'm willing to try any mode which gets the desired behaviour though! \$\endgroup\$ – dougajmcdonald Sep 15 '16 at 7:04
1
\$\begingroup\$

If you are using Screen Space, you shouldn't worry about where you see your text in the scene, as the position of the canvas elements will most likely not match the scene positions and scale, since it's using its own position and scale reference to display stuff on the screen.

But from your screenshot, you can see the canvas and the Text are disabled in the hierarchy. Since the problem is in the clients, maybe you are enabling the UI in a script that is only running in the server, or maybe you added the Network Identity component to it, and set it to "Sever only", which disables the gameObject on clients.

\$\endgroup\$
  • \$\begingroup\$ Hi Leo, I do have the text object causing the problem set with a network identity as I need it to be updated on the client by the server. But I've not set it to server only. I've not set it to client authority either, should I set this in a particular way? \$\endgroup\$ – dougajmcdonald Sep 15 '16 at 8:21
  • 1
    \$\begingroup\$ Well, I wouldn't set anything from the UI as a network identity. I would create another gameobject with a script that keeps track of stuff that need to be networked, like the text you want to sync. This way it's easier to track errors, as you keep the display and data sync on separate modules, which makes everything easier to debug. \$\endgroup\$ – Leo Sep 15 '16 at 8:54
  • 1
    \$\begingroup\$ Leo is right , UI elements work as Viewers like in MVC pattern , you don't have to add network identity to them. \$\endgroup\$ – Dr.MSM Sep 15 '16 at 14:33
  • \$\begingroup\$ This makes a lot of sense, just trying to picture the architecture. At the moment I'm seeing a server side game state, a mechanism to send the time to the client which would be networked (so a client copy of the game state?) and then some way to bind the client game state to the UI? Would this be about right? \$\endgroup\$ – dougajmcdonald Sep 15 '16 at 19:47
  • \$\begingroup\$ Marking this as accepted, will edit question with final solution \$\endgroup\$ – dougajmcdonald Sep 16 '16 at 7:09
1
\$\begingroup\$

In what mode are you rendering the canvas , "Screen space overlay" or "Screen space camera" , if in camera mode you may consider that your scene will have 2 cameras and the canvas will recognize only the host camera , so you must disable the other player camera , using unity OnClientCreate() or whatever UNET call it.

\$\endgroup\$
  • \$\begingroup\$ My UI canvas is using screen space overaly. What's odd is that I have a textLabel (which is static) and appears in the same place on each game instance. And a textValue which is updated within the game loop and moves on the client. \$\endgroup\$ – dougajmcdonald Sep 15 '16 at 7:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.