question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem with Spawning at runtime and with NetworkedTransform

See original GitHub issue

Hi,

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:

  1. although the Jenga tower is properly constructed on the Host, this is not the case for the Client.

  2. 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:closed
  • Created 5 years ago
  • Comments:87 (50 by maintainers)

github_iconTop GitHub Comments

2reactions
TwoTenPvPcommented, May 2, 2018

Cough NetworkedBehaviour.GetNetworkedObject(uint networkId)

2reactions
TwoTenPvPcommented, Apr 27, 2018

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”.

Moreover, I don't get it why does MLAPI work well in syncing a single object that I move with my clients, but not with a more complicated construction like the Jenga tower. And it's quite weird that in the Jenga tower example, when I manipulate some blocks they are immediately updated correctly on the Server, but the updated positions seem to not be passed correctly on the client, and therefore I see some jittering behavior.

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Problem with Spawning at runtime and ...
So issue number 1 is related to the spawning of objects that are spawned during runtime? And number two is an issue with...
Read more >
Resolved - Problem spawning prefabs with NetCode
the object which holds this script is not spawned with OwnerShip, in that case, you PROBABLY used Instantiate(). Spawn() to spawn it.
Read more >
Unity network transform problem - Client spawning objects ...
I'm working on a Fortnite-esque game for Unity. Player spawns in, has ability to spawn cubes to make a "base". Everything works perfectly...
Read more >
Changelog | Netcode for GameObjects | 1.5.2
NetworkHide() of an object that was just NetworkShow() n produces a warning, as remote clients will not get a spawn/despawn pair. Renamed the...
Read more >
Spawn problem in multiplayer
Hello I am struggling with figuring out the following problem: Game starts, player chooses a team (just a widget button that sets a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found