Problem with Spawning at runtime and with NetworkedTransform
See original GitHub issueHi,
I try to implement a jenga tower. So, I have introduced a JengaBlock prefab. I have added the Networked Object, the Tracked Object, the Networked Transform, and the Rigidbody component. I have also included this prefab in the Networked Prefabs of my Network Manager. Then I have created an empty object and named it Spawn Networked Objects. With the Spawn Networked Objects selected I added a new script and named it SpawnNetworkedObjects.cs. For convenience I’m attaching you the script so you can reproduce the buggy behavior I get.
using MLAPI;
using MLAPI.Data;
using MLAPI.MonoBehaviours.Core;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnNetworkedObjects : NetworkedBehaviour
{
public GameObject JengaBlock;
private GameObject[] JengaBlocks;
const int height = 8;
// This is equivalent to overriding OnStartServer()
private void Start()
{
JengaBlocks = new GameObject[height * 3];
NetworkingManager.singleton.OnServerStarted = makeJengaBlocks;
}
void makeJengaBlocks()
{
float xDistPercentage = 0.5f;
float yDistPercentage = 0.4f;
int i = 0;
for (int y = 0; y < height; y++)
{
for (int x = -1; x <= 1; x++)
{
if ((y & 1) == 0)
{
JengaBlocks[i] = Instantiate(JengaBlock, new Vector3(x * xDistPercentage, y * yDistPercentage, 0.0f), Quaternion.identity);
JengaBlocks[i].GetComponent<NetworkedObject>().Spawn();
}
else
{
JengaBlocks[i] = Instantiate(JengaBlock, new Vector3(0.0f, y * yDistPercentage, x * xDistPercentage), Quaternion.Euler(0.0f, 90.0f, 0.0f));
JengaBlocks[i].GetComponent<NetworkedObject>().Spawn();
}
i++;
}
}
}
}
There are two problems here:
-
although the Jenga tower is properly constructed on the Host, this is not the case for the Client.
-
When I interact with the Jenga blocks in one of the Clients, the blocks seem to not be synchronized between the Client and the Host.
How can I fix these issues? Is it something I’m doing so wrong here?
Thanks!
Issue Analytics
- State:
- Created 5 years ago
- Comments:87 (50 by maintainers)
Top GitHub Comments
Cough NetworkedBehaviour.GetNetworkedObject(uint networkId)
I’m assuming the reason is that you are calling ChangeOwnership and RemoveOwnership on the client. That’s not allowed. I will add checks for that. Ownership takes time to take effect. It needs to be sent across. It’s not something you do in “realtime”.
This is not the MLAPI’s job. The MLAPI is here to offer abstractions. To allow you to send messages for MonoBehaviours (NetworkedBehaviours), to allow spawning objects across instances etc. Syncing positions and doing logic, that’s what the game developer’s job is. The NetworkedTransform is a prototype component as stated in the Wiki. It’s designed with one purpose. To Sync position from Client to server, or Server to client. If you want more complicated behaviour, write it.
That’s the biggest problem about “Prototype components”. They are used as production. If they don’t fit your usecase, you have to write your own.