- Home /
Question by
CloudiaNBusiness · Feb 04, 2019 at 06:03 PM ·
unity 5networkingnetworknetwork instantiate
Gameobject only spawns on hosts game. Not on the clients.
I'm having some issues with the networking of spawning gameobjects on to the scene. Can someone explain what the problem is and why this does not spawn it on the scene of everyones game?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class PlayerObject : NetworkBehaviour {
public GameObject playerTextPrefab;
public GameObject parrent;
public string _ID;
public float netWorth;
public float money;
// Use this for initialization
void Start () {
if (isLocalPlayer)
{
CmdSpawnMyText();
}
_ID = GetComponent<NetworkIdentity>().netId.ToString();
transform.name = "Player"+_ID;
}
public GameObject myText;
// Update is called once per frame
void Update () {
//Runs on all player units
if (isLocalPlayer)
{
netWorth = MoneyHandler.netWorth;
money = MoneyHandler.money;
CmdUpdateText(netWorth, money);
}
}
[Command]
void CmdSpawnMyText()
{
parrent = GameObject.Find("Users").gameObject;
GameObject go = Instantiate(playerTextPrefab);
go.transform.parent = parrent.transform;
myText = go;
myText.transform.localScale = new Vector3(1, 1, 1);
myText.transform.localPosition = new Vector3(-13, 43f, 0f);
myText.transform.localRotation = Quaternion.Euler(0, 0, 0);
//SEND TO ALL THE CLIENTS
NetworkServer.Spawn(myText);
}
[Command]
void CmdUpdateText(float networth, float moneyin)
{
myText.GetComponent<Text>().text = networth.ToString();
money = moneyin;
netWorth = networth;
if (isServer) RpcUpdateText(networth, moneyin);
}
[ClientRpc]
void RpcUpdateText(float networth, float moneyin)
{
if (!isLocalPlayer)
{
money = moneyin;
netWorth = networth;
}
}
}
Comment