-
Notifications
You must be signed in to change notification settings - Fork 447
Unable to parent NetworkObject to a NetworkObject only on client #3100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@Conundroy Order of Operations:
Let me know if this resolves your issue or if not then if you could provide me with a replication project or the script that you are using to instantiate, spawn, and parent the two objects it would be helpful. |
I have the same issue, it started appearing when I upgraded from 1.8 to 2.0.
The object gets correctly parented on server and also late joining clients, but for clients currently in the game the parenting never happens. |
Here's the script on the parent object/game manager:
This works as intended in v1.11.0. |
Hmmm...generally speaking...instantiating and spawning in the middle of spawning is not recommended as it can lead to issues down the road. Just out of curiosity, have you tried changing the |
This is the first time I'm hearing that its not recommended to spawn things inside OnNetworkSpawn. How are you supposed to spawn things on server when it initializes then? I did also try OnNetworkPostSpawn for this specific issue, and it didn't change anything. |
@Fobri |
Have you tried setting the transform on a NetworkBehaviour (OtherNetworkBehaviour below) of the instantiated prefab and parenting it within the OnNetworkPostSpawn of the NetworkBehaviour on the newly instantiated prefab? override void OnNetworkSpawn()
{
var instance = Instantiate(prefab);
instance.GetComponent<OtherNetworkBehaviour>.SetTargetParent(transform);
instance.GetComponent<NetworkObject>().Spawn();
} And then on OtherNetworkBehaviour: private Transform m_TargetParent;
public void SetTargetParent(Transform targetParent)
{
m_TargetParent = targetParent;
}
protected override void OnNetworkPostSpawn()
{
if (IsServer)
{
NetworkObject.TrySetParent(m_TargetParent);
}
base.OnNetworkPostSpawn();
} This will assure that when you are parenting the newly spawned object instance is fully spawned prior to parenting it. |
I have the same issue. If you spawn something and then on the client make a serverrpc call to set the parent, the parent will change on the server and client for all available clients and late joiners. However, if the server does the parent change on its own (without a serverrpc) the parent only changes on the server and not on any already available clients (late joiners will have the correct parent though). |
@marcusx2 Do you have the script of how your server is doing the parent change? If the client is not fully connected and synchronized when the parent is changed then the parent change message can be lost. |
@EmandM It's nothing special. It's a NetworkBehaviour script attached to a network object. I check if it's the server or if it's the client OnNetworkSpawn. If it's the server I spawn and call TrySetParent, that is all. The parenting doesn't work for clients that are already connected, only late joiners. Maybe if I use NetworkPostSpawn? Haven't tried it. But anyways, it's a bug. It doesn't make sense to think that the message can be lost, when late joiners are correctly reparented. |
@marcusx2 @Conundroy |
This PR resolves the issue where instantiating and spawning a `NetworkObject` (A) during another `NetworkObject`'s spawn process (B) and then parenting the newly spawned `NetworkObject` (A) under the `NetworkObject` (B) would end up causing the parenting message to not properly defer the parenting message until the parent (B) was fully spawned on other already connected and synchronized clients (it would properly parent if another client late joined shortly after). [MTTB-1209](https://jira.unity3d.com/browse/MTTB-1209) fix: #3100 ## Changelog - Fixed: Issue where during a `NetworkObject` spawn if you instantiated, spawned, and parented another network prefab under the currently spawning `NetworkObject` the parenting message would not properly defer until the parent `NetworkObject` was spawned. ## Testing and Documentation - Includes new integration ParentingDuringSpawnTests test. - No documentation changes or additions were necessary. <!-- Uncomment and mark items off with a * if this PR deprecates any API: ### Deprecated API - [ ] An `[Obsolete]` attribute was added along with a `(RemovedAfter yyyy-mm-dd)` entry. - [ ] An [api updater] was added. - [ ] Deprecation of the API is explained in the CHANGELOG. - [ ] The users can understand why this API was removed and what they should use instead. --> ## Backport This requires a backport to v1.x as the `ParentSyncMessage` does not defer the message if the parent `NetworkObject` is not spawned yet.
Closing as #3401 resolved this issue. |
Description
Unable to parent NetworkObject to a NetworkObject only on client.
Reproduce Steps
Actual Outcome
Host:
Client:
Expected Outcome
Host:
Client:
Environment
Additional Context
Works if I spawn and parent in editor, probably has something to do with DesroyedWithScene set to true?
Update: Works when I downgrade to v1.11.0
The text was updated successfully, but these errors were encountered: