Model spawned under avatar not syncing properly
Hi, quick question. I'm developing an inventory system. The inventory spawns as a child of the player Avatar. The current test item is a megaphone, which has a realtime model to sync some logic. The issue is the megaphone model works fine when it is in the world, but does not sync when it is spawned with the avatar. Any ideas?
20 Replies
Hi @Octoyisus. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
When a model (like your megaphone) is spawned as a child of the player avatar and does not sync properly, there are a few common issues in Normcore to check:
1. Instantiation Method:
Make sure you are using
Confirm that your megaphone prefab has a
Reparenting objects with RealtimeView/RealtimeComponent at runtime is not recommended, as Normcore does not synchronize parent relationships. If you need to attach the megaphone to the avatar, do not reparent it directly. Instead, synchronize its position to follow the avatar using a script, or use an empty GameObject as a reference point RealtimeView Common Questions. 4. Prefab Integrity:
If the prefab is corrupt or the RealtimeView/RealtimeComponent relationship is broken, the model will not be created or synced. Double-check the prefab in the Unity editor and ensure all components are correctly set up RealtimeComponents Common Questions. 5. Initialization Timing:
If you’re accessing the model in
- Always use
Make sure you are using
Realtime.Instantiate()
to spawn the megaphone, not Unity’s standard Instantiate()
. Only Realtime.Instantiate()
ensures the object and its RealtimeComponents are networked and synchronized across all clients. Using Unity’s Instantiate()
will result in the model not syncing, as the RealtimeComponent’s model will remain null and not be connected to the datastore RealtimeComponents Common Questions.
2. RealtimeView and RealtimeComponent Setup:Confirm that your megaphone prefab has a
RealtimeView
and that your custom RealtimeComponent (the one syncing the logic) is listed in the RealtimeView’s component list. If it’s missing, remove and re-add the component to the prefab to force Unity to update the metadata Synchronizing Custom Data FAQ.
3. Parenting and Ownership:Reparenting objects with RealtimeView/RealtimeComponent at runtime is not recommended, as Normcore does not synchronize parent relationships. If you need to attach the megaphone to the avatar, do not reparent it directly. Instead, synchronize its position to follow the avatar using a script, or use an empty GameObject as a reference point RealtimeView Common Questions. 4. Prefab Integrity:
If the prefab is corrupt or the RealtimeView/RealtimeComponent relationship is broken, the model will not be created or synced. Double-check the prefab in the Unity editor and ensure all components are correctly set up RealtimeComponents Common Questions. 5. Initialization Timing:
If you’re accessing the model in
Awake()
or Start()
, it may not be ready yet. Move your initialization logic to OnRealtimeModelReplaced()
to ensure the model is available RealtimeComponent.
Summary:- Always use
Realtime.Instantiate()
for networked objects.
- Avoid reparenting networked objects at runtime.
- Ensure your prefab setup is correct and initialization logic is in OnRealtimeModelReplaced()
.
If you follow these steps and the issue persists, please provide more details about how you’re spawning and attaching the megaphone, and how your RealtimeComponent is set up.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@286884711874166784> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
you mean the position? cos reparenting is not supported in RealtimeTransform
No sorry
I mean the items custom logic
The position is handled by a realtime transform attached
I m doing reparenting, just to separate the inventory from the avatar in the hierarchy, but it's just a parent removal done in all clients
and the whole stack is owned by the same player?
the root view ownership overrides any child ownership
The whole stack is owned by the same player, i was gonna ask, should i add a meta model to m component so i can take ownership of the component itself?
depends what the goal is
Can I unparent the view of the item from the one from the avatar at runtime?
you can take control of a RT without taking ownership of the parent view so other players can take ownership of the RT for instance
I think the general rule is you don't change parents of synced objects but it might work
But do you think changing the parent might break something? I was under the impresion it would not, just that the change would not sync
I haven't tried but it does show a list of child views in the component so it's something that's tracked, maybe it updates that list now
After the reparenting the child is kept on the avatar's list, so that s probably not the issue
can you share the code that's not working?
Sure, but it's gonna be a little convoluted, give me a minute
I'm gonna start with the item, as i said it's a megaphone, this all works fine if it's already on the scene before the game starts:
Since I'm using the vrif framework, I needed to inherit from a certain class to control the player input, so the model is accessed through an auxiliary class (MegaphoneSync).
I'd try using Realtime.Instantiate instead of unparenting it
you can keep the current setup but hide the one in the inventory when you drop it and Realtime.Instantiate a new megaphone
Ok, ill try instancing the inventory prefab when an avatar is created and let you know
I've tried, no luck. Still the same issue. The model's callbacks are not being called on by the other clients, but they're being called on by the owners. I'm going to print to see if the values change in the model.
are you trying the change the values on the model owned by another player?
I think it's working! It worked when I started instancing the inventory, but I missed some logic that was previously on start, and now it is needed in OnRealtimeModelReplaced.
I would prefer to be able to have the inventory as a child of the avatar, but it's not that big of a deal. Truly thankful to you right now.