- Home /
How to network canvas elements ??
Helllo,
I am developing networking quiz game which 2 players are getting one question with 4 answers.
I want to network the question and its answers in the client but i can't do that. i handled answers like enemies or non-player objects in unity tutorial but no point
How can i do that ?? Also i can't network the text of the question ??
So i want to ask should i handle canvas ui elements with another method or what ?
Thanks in advance :)
Answer by Eslam-Mohamed · May 17, 2018 at 12:15 PM
I might be mistaken but when you create the player prefab you put the canvas inside as a child. After creating the Canvas object you uncheck his Canvas component(The canvas should disapear from the scene) Then you create a script and put them as a component of your player object prefab. The script should containn the following:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class OnlyLocalCanvas : NetworkBehaviour {
// Use this for initialization
void Start()
{
if (isLocalPlayer)
{
GetComponentInChildren<Canvas>().enabled = true;
}
}
}
Note that the script has a using UnityEngine.Networking; and tthe class is NetworkBehaviour insted of MonoBehaviour.
You should be good to go!
What this does is when a player spawns in the Network that player (And only that player) can see the Canvas! By default the others players canvas are disabled! it only enables yours [ if(isLocalPlayer) ]
This is the corrent answer, but to be clear, the Canvas CO$$anonymous$$PONENT of the Canvas UI is what needs to be disabled.
Answer by sennin31 · Oct 17, 2016 at 03:00 PM
Hi,
I think the best idea will be to have a server and an API. The server will be the game manager, and will send questions/answers to remote clients (with json, xml, or whatever you want). Everything will be synchronize and manage by the server.
Now i am just beginner so i want to use one client as a host. The problem is i can't synchronize any way.
Check this doc : https://docs.unity3d.com/$$anonymous$$anual/UNet$$anonymous$$essages.html
You will be able to send integer. This integer could be an index to an array shared in each app. This array could contains questions or/and answers on each client, and the index would allow you to synchronise your UI
Can i synchronize ui group of answers with there texts and events ??
Thank you for help at all .. please assist me because i got headache for days on this issue.
Look at this one : https://unity3d.com/fr/learn/tutorials/topics/multiplayer-networking/networking-player-health?playlist=29690
wich is part of those tutorials : https://unity3d.com/fr/learn/tutorials/topics/multiplayer-networking
You can use [SyncVar] on your index
Your answer
Follow this Question
Related Questions
Is it possible to make an online multiplayer 2D game in unity using only the unity UI components 1 Answer
UI Element Transform being set incorrectly 1 Answer
Menu not getting Keyboard Input 0 Answers
Why are my GUI.Button not rendering when running my networked game? 0 Answers
How to change the text of a button with a name from a list without the name repeating? 2 Answers