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
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.
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.
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.
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:
----------------------<@391444583034257420> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
You’re most likely calling Destroy on multiple clients on the same model
It might be worth only having the owner do it
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?
sorry I mean Realtime.Destroy is being called. Unity calling GameObject.Destroy won’t cause this bug