BabainB
Normal2y ago
24 replies
Babain

Realtime Avatar manager avatars count issue.

When i connect to a room that already has another connected player, i check for the realtimeavatarmanager.avatars.count inside DidConnectToRoom but it is 0 even though the connecting player ends up in the same room as the already connected one. What could possibly be the origin of this issue?
Solution
----------------------
The issue you're experiencing might be due to the timing of when you're checking the avatars count. The DidConnectToRoom event is fired as soon as the connection to the room is established, but it doesn't necessarily mean that all the avatars have been instantiated and registered with the RealtimeAvatarManager at that point.

The RealtimeAvatarManager keeps track of all avatars in the room via the localAvatar and avatars properties. The avatars property is a dictionary of all RealtimeAvatar components stored under the clientID that the avatar belongs to. This is useful for determining how many players are currently in a multiplayer space. However, the avatars might not be instantiated immediately upon connection.

If you want to be notified when players join and leave, RealtimeAvatarManager includes two events: avatarCreated and avatarDestroyed. You can use these events to keep track of the avatars in the room. Here's an example of how to use these events:

using UnityEngine;  
using Normal.Realtime;  
  
public class MyAvatarManager : MonoBehaviour {  
 private RealtimeAvatarManager _manager;  
  
 private void Awake() {  
 _manager = GetComponent<RealtimeAvatarManager>();  
 _manager.avatarCreated += AvatarCreated;  
 _manager.avatarDestroyed += AvatarDestroyed;  
 }  
  
 private void AvatarCreated(RealtimeAvatarManager avatarManager, RealtimeAvatar avatar, bool isLocalAvatar) {  
 // Avatar created!  
 }  
  
 private void AvatarDestroyed(RealtimeAvatarManager avatarManager, RealtimeAvatar avatar, bool isLocalAvatar) {  
 // Avatar destroyed!  
 }  
}  


In this example, the AvatarCreated and AvatarDestroyed methods will be called whenever an avatar is created or destroyed, respectively. You can use these methods to keep track of the number of avatars in the room.
Was this page helpful?