- Home /
Question by
fet0r · Jan 09, 2018 at 01:00 AM ·
networkingmultiplayer-networkingspawning problemsclient-serverclients
UNET Different size of spawned object on host and client
Hello If I spawn the object from host, on other Clients is bigger.
Only local player can spawn objects. Console shows the error when client tries to spawn block:
"NullReferenceException: Object reference not set to an instance of an object GameManager.Update () (at Assets/Script/GameManager.cs:87)"
using UnityEngine;
using System.Collections;
using System;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using UnityEngine.Networking;
public class Block
{
public Transform blockTransform;
}
public struct BlockAction
{
public bool delete;
public Vector3 index;
}
public class GameManager : NetworkBehaviour
{
public static GameManager Instance { set; get; }
private float blockSize = 0.25f;
public Block[,,] blocks = new Block[100, 100,100];
public GameObject blockPrefab;
private GameObject foundationObject;
private Vector3 blockOffset;
private Vector3 foundationCenter = new Vector3(10f, 0, 10f);
[SyncVar]
private bool isDeleting;
public Button deleteButton;
public Sprite[] deleteButtons;
[SyncVar]
private int blockIndex = 0;
private void Start()
{
Instance = this;
foundationObject = GameObject.Find("Foundation");
blockOffset = (Vector3.one * 0.5f) / 4;
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 30.0f))
{
if (isDeleting)
{
if(hit.transform.name != "Foundation")
{
Vector3 oldCubeIndex = BlockPosition(hit.point - (hit.normal * (blockSize - 0.01f)));
NetworkServer.Destroy(blocks[(int)oldCubeIndex.x, (int)oldCubeIndex.y, (int)oldCubeIndex.z].blockTransform.gameObject);
blocks[(int)oldCubeIndex.x, (int)oldCubeIndex.y, (int)oldCubeIndex.z] = null;
}
return;
}
Vector3 index = BlockPosition(hit.point);
int x = (int)index.x
, y = (int)index.y
, z = (int)index.z;
if (blocks[x, y, z] == null)
{
CmdCreateBlock (index);
GameObject go = GameObject.Find ("Block" + (blockIndex - 1));
blocks[x, y, z] = new Block
{
blockTransform = go.transform,
};
}
else
{
CmdCreateBlock (index);
GameObject go = GameObject.Find ("Block" + (blockIndex - 1));
Vector3 newIndex = BlockPosition(hit.point + (hit.normal * blockSize));
blocks[(int)newIndex.x, (int)newIndex.y, (int)newIndex.z] = new Block
{
blockTransform = go.transform,
};
PositionBlock(go.transform, newIndex);
}
}
}
}
[Command]
void CmdCreateBlock(Vector3 index)
{
GameObject go = Instantiate(blockPrefab) as GameObject;
go.name = "Block" + blockIndex.ToString();
go.transform.position = ((index * blockSize) + blockOffset) + (foundationObject.transform.position - foundationCenter);
go.transform.localScale = Vector3.one * blockSize;
blockIndex += 1;
NetworkServer.Spawn (go);
}
private Vector3 BlockPosition(Vector3 hit)
{
int x = (int)(hit.x / blockSize);
int y = (int)(hit.y / blockSize);
int z = (int)(hit.z / blockSize);
return new Vector3(x,y,z);
}
public void PositionBlock(Transform t, Vector3 index)
{
t.position = ((index * blockSize) + blockOffset) + (foundationObject.transform.position - foundationCenter);
}
public void ToggleDelete()
{
isDeleting = !isDeleting;
deleteButton.image.sprite = (!isDeleting) ? deleteButtons[0] : deleteButtons[1];
}
}
screen2.jpg
(14.5 kB)
screen1.png
(188.1 kB)
Comment