Rex G
Rex G3mo ago

Sync Headphone being viewable

I am struggling to make it so everyone sees a gameobject over the players head go active and inactive. Here is my script using the example your document have provided. I just need everyone to see when it is active and inactive. Everyone has this gameobject on their rig and they will be making it active and inactive throughout the room.
35 Replies
kapa.ai
kapa.ai3mo ago
Hi @Rex G. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
kapa.ai
kapa.ai3mo ago
---------------------- To synchronize the active/inactive state of a GameObject (like a headphone icon above a player's head) so that all users in the room see the same state, you need to synchronize this state using a custom RealtimeComponent and model in Normcore. The built-in components (like RealtimeTransform) only sync position, rotation, and scale—not active state. Here’s how you can approach this, based on the official guide for synchronizing custom data: 1. Create a custom model to store the active state (e.g., a boolean). 2. Create a custom RealtimeComponent that updates the GameObject’s active state based on the model and updates the model when the local user changes the state. Example 1. Create the model:
using Normal.Realtime;
using Normal.Realtime.Serialization;
using UnityEngine;

[RealtimeModel]
public partial class HeadphoneStateModel {
[RealtimeProperty(1, true, true)]
private bool _isActive;
}
using Normal.Realtime;
using Normal.Realtime.Serialization;
using UnityEngine;

[RealtimeModel]
public partial class HeadphoneStateModel {
[RealtimeProperty(1, true, true)]
private bool _isActive;
}
After creating this, use the Normcore inspector to compile the model code as described in the docs. This will generate the code needed to sync the property across the network [Synchronizing Custom Data]. 2. Create the RealtimeComponent:
using UnityEngine;
using Normal.Realtime;

public class HeadphoneStateSync : RealtimeComponent<HeadphoneStateModel> {
[SerializeField] private GameObject headphoneObject;

protected override void OnRealtimeModelReplaced(HeadphoneStateModel previousModel, HeadphoneStateModel currentModel) {
if (previousModel != null) {
previousModel.isActiveDidChange -= OnIsActiveChanged;
}
if (currentModel != null) {
if (currentModel.isFreshModel)
currentModel.isActive = headphoneObject.activeSelf;
UpdateHeadphoneState();
currentModel.isActiveDidChange += OnIsActiveChanged;
}
}

private void OnIsActiveChanged(HeadphoneStateModel model, bool isActive) {
UpdateHeadphoneState();
}

private void UpdateHeadphoneState() {
headphoneObject.SetActive(model.isActive);
}

public void SetHeadphoneActive(bool active) {
model.isActive = active;
}
}
using UnityEngine;
using Normal.Realtime;

public class HeadphoneStateSync : RealtimeComponent<HeadphoneStateModel> {
[SerializeField] private GameObject headphoneObject;

protected override void OnRealtimeModelReplaced(HeadphoneStateModel previousModel, HeadphoneStateModel currentModel) {
if (previousModel != null) {
previousModel.isActiveDidChange -= OnIsActiveChanged;
}
if (currentModel != null) {
if (currentModel.isFreshModel)
currentModel.isActive = headphoneObject.activeSelf;
UpdateHeadphoneState();
currentModel.isActiveDidChange += OnIsActiveChanged;
}
}

private void OnIsActiveChanged(HeadphoneStateModel model, bool isActive) {
UpdateHeadphoneState();
}

private void UpdateHeadphoneState() {
headphoneObject.SetActive(model.isActive);
}

public void SetHeadphoneActive(bool active) {
model.isActive = active;
}
}
- Attach this script to the same GameObject as your headphone icon. - Assign the headphone GameObject in the inspector. - Call SetHeadphoneActive(true/false) when you want to change the state. This will ensure that when any user changes the headphone state, it is synchronized for all users in the room [Synchronizing Custom Data]. Note: Make sure your prefab has a RealtimeView on the root and is instantiated using Realtime.Instantiate() if you want it to be networked [Realtime API]. If you need more details on compiling the model or wiring up the prefab, see the official guide: Synchronizing Custom Data. If you have further issues, please provide your current script and setup for more targeted help! ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3mo ago
<@918869683929768036> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
NormalMark
NormalMark3mo ago
this looks correct ^ how are you activating the headphones on the owner client, is it by calling SetActiveState?
Rex G
Rex GOP3mo ago
Yes by clicking a button Do I have to set ownership?
Mechabit
Mechabit3mo ago
if the headphones are on the player avatar then only the owners can change the model, you can't take ownership of other people's avatar
Rex G
Rex GOP3mo ago
I don't need to take ownership. I just need for everyone to see everyone's headphones on their head in real time being set active and inactive
Mechabit
Mechabit3mo ago
you can try setting the headphones as a child of this game object so you're not disabling the script at the same time and only setting the headphones active state
Rex G
Rex GOP3mo ago
Basically I need to give the effect a pair of headphones appear on their head when they click a music choice on a playlist in the app and every needs to see the headphones on their head and when they close the playlist the headphones go back to inactive
Rex G
Rex GOP3mo ago
The headphones is attached to the center eye anchor of the OVR Camera Rig here
No description
Mechabit
Mechabit3mo ago
is that rig the avatar that gets spawned by Realtime? cos if it's in the scene then that view will be shared by all players what you want to do is either have the headphones on the avatar that gets spawned or just Realtime.Instantiate the headphones at runtime separately
Rex G
Rex GOP3mo ago
Attach it to this? The Meta Avatar Prefab? But how do I set it into inactive and active state?
No description
Mechabit
Mechabit3mo ago
yeah it should be on that the ovr rig shouldn't have any network stuff on it
Rex G
Rex GOP3mo ago
Ok so how in my scene can I access the script attached to toggle it to active and inactive state?
Mechabit
Mechabit3mo ago
you can find your local avatar in Avatar Manager or add a isOwnedLocallyInHeirarchy check to the start function of the headphones
Rex G
Rex GOP3mo ago
I need to be able to call the method on this Interactable Unity Event Wrapper because this is the scriipt attached to object they are clicking to change the state.
No description
Rex G
Rex GOP3mo ago
Here is where it is checked in the start function
Mechabit
Mechabit3mo ago
You can edit your headphones script to include a static variable called localInstance which is assigned in Start if isOwnedLocallyInHierarchy is true Then add a script to your event object that fires the set active function on your static variable
Rex G
Rex GOP3mo ago
So attach this to the gameobject with the Interactable unity event wrapper correct?
Rex G
Rex GOP3mo ago
No description
Rex G
Rex GOP3mo ago
You probably cant see it but I got it to appear on start when their avatar loads now I need to be able to set it inactive when they click an object using Interactable unity event wrapper from that headphone prefab
Rex G
Rex GOP3mo ago
I added the static variable but when I try to click the gameobject with Interactable event wrapper the headphones don't become active or inactive. I need it to start as inactive and when they click that gameobject it becomes active and vice versa ??? @Mechabit
Mechabit
Mechabit3mo ago
looks fine to me, you should add some debug logs to see when the method gets called
Rex G
Rex GOP3mo ago
Finally figured this out but one question. I need the headphone to be stuck to the individual transform of the joint head for each player that is generated on the Normal Meta Avatar. How can I do that?
Mechabit
Mechabit3mo ago
use a script to move the headphone every frame
Rex G
Rex GOP2mo ago
I am running into issues with these three scripts. When a player presses a button to show their headphones GameObject, it should become visible for everyone else too—but it shouldn't affect visibility on other players' avatars. Right now, it's only toggling locally and not syncing across the network. I need help syncing per-player headphone visibility so others can see it.
Mechabit
Mechabit2mo ago
doesn't look like you're calling ToggleHeadphones ? add a debug log to see when something is called
Rex G
Rex GOP2mo ago
Got it to work One issue someone else in the server sees the gameobject is in the position on their head like I need it to but from the players view it looks like the object is floating above their head. I have a realtime view and realtime transform on the gameobject I need
Rex G
Rex GOP2mo ago
Any help with this? It shows up in the right spot on the inspector. I made sure to parent it with the Joint Head so it is at the transform of the Joint Head that spawns at runtime on the Normal Meta Avatar but on the headset it appears either on the floor or next to the player. It needs to fit onto the head like a pair of headphones
No description
Mechabit
Mechabit2mo ago
Realtime Transform doesn't support parenting you have to move the headphones to the correct position every frame or if you only ever need the headphones on the person's head and don't need Realtime Transform make sure it is reparented on all remote clients cos that doesn't happen automatically
Rex G
Rex GOP2mo ago
Ok the headphones just need to keep its position on that players head so everyone can see it. Thats why I kept the realtime transform because when testing everyone could see when someone switched their headphones to an active state. I just want to make sure if I remove realtime transform that doesn't change
Mechabit
Mechabit2mo ago
you don't need RT for that
Rex G
Rex GOP2mo ago
Ok

Did you find this page helpful?