jos_valentin
jos_valentin4w ago

Error when destroy and instantiate realtime components with same model

When changing a scene, all objects are destroyed, so for example my realtime component PlayerMovement is destroyed, then in the new scene I use the the same PlayerMovement component for another prefab, there are no issues on playing, but I get the error log saying that the model of that component is not in the datastore
5 Replies
kapa.ai
kapa.ai4w ago
Hi @jos_valentin. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- I understand you're encountering an error when destroying and instantiating Realtime components with the same model across different scenes. This is a common issue with Normcore's datastore management. When you change scenes in Unity, all objects are destroyed, including your Realtime components. The error you're seeing about the model not being in the datastore occurs because Normcore is trying to reference a model that was removed from the datastore when the previous scene was unloaded. This happens because when you destroy a GameObject with a RealtimeView, Normcore removes its associated models from the datastore. Then, when you try to use the same component type in a new scene, it's looking for the previous model which no longer exists. Here are some approaches to solve this issue: 1. Use Realtime.Instantiate() for prefabs: Make sure you're using Normcore's instantiation method rather than Unity's standard Instantiate.
GameObject myObject = Realtime.Instantiate("My Object", ownedByClient: true);
GameObject myObject = Realtime.Instantiate("My Object", ownedByClient: true);
Ownership and Lifetime Flags 2. Wait for connection: For RealtimeViews in scene files, make sure you wait until realtime.connected == true or the didConnectToRoom event has fired before interacting with them.
realtime.didConnectToRoom += OnConnectedToRoom;

private void OnConnectedToRoom() {
// Now it's safe to interact with RealtimeViews
}
realtime.didConnectToRoom += OnConnectedToRoom;

private void OnConnectedToRoom() {
// Now it's safe to interact with RealtimeViews
}
Common Questions 3. Consider using persistent objects: If you need the same component across scenes, you might want to use DontDestroyOnLoad() on the GameObject containing your Realtime components, or use lifetime flags to make objects persist.
GameObject myPersistentObject = Realtime.Instantiate("My Object", destroyWhenOwnerOrLastClientLeaves: false);
GameObject myPersistentObject = Realtime.Instantiate("My Object", destroyWhenOwnerOrLastClientLeaves: false);
Lifetime Flags 4. Initialize in OnRealtimeModelReplaced(): Make sure your component initialization logic is in the OnRealtimeModelReplaced() method rather than in Awake() or Start(). The knowledge sources don't provide a specific solution for your exact scenario, but these approaches should help address the issue. If you continue to experience problems, you might need to implement a more custom solution for managing your Realtime components across scene changes. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4w ago
<@391444583034257420> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
maxweisel
maxweisel4w ago
You’re most likely calling Destroy on multiple clients on the same model It might be worth only having the owner do it
jos_valentin
jos_valentinOP4w ago
So when I load a scene, the Destroy is called for every realtime object I have in the previous scene? how can I handle it?
maxweisel
maxweisel4w ago
sorry I mean Realtime.Destroy is being called. Unity calling GameObject.Destroy won’t cause this bug

Did you find this page helpful?