General unity callbacks ready to use with model?
When I spawn a realtime object, e.g., with addressables, is it guaranteed that the model will be connected and working in Start() and Update()? Also in Awake()?
7 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:
----------------------
When you spawn a realtime object using
Realtime.Instantiate()
(including with Addressables, as long as you use the correct delegate), the initialization order is important:
- Awake(): The model is not guaranteed to be available in Awake()
. At this point, Unity has just created the GameObject, but Normcore has not yet connected the model to your RealtimeComponent
. You should not access the model here.
- Start(): For prefabs instantiated with Realtime.Instantiate()
, the model is guaranteed to be available in Start()
. All views and components are initialized with their models before Start()
is called on any script on the prefab. This means you can safely use the model in Start()
and Update()
for prefabs.
- Update(): The model is available, as long as it was set up in Start()
.
For scene objects (not prefabs), you must wait until realtime.connected == true
or the didConnectToRoom
event has fired before the model is guaranteed to be available. For prefabs, you do not need to wait—Start()
is sufficient.
Summary Table:
| Callback | Model Available (Prefab)? | Model Available (Scene Object)? |
|------------|--------------------------|-----------------------------------------|
| Awake() | ❌ No | ❌ No |
| Start() | ✅ Yes | ❌ (wait for didConnectToRoom) |
| Update() | ✅ Yes | Only after didConnectToRoom |
References:
- RealtimeComponent lifecycle
- Common Questions: Model availability
- Addressables instantiation
If you are using Addressables, as long as you use Realtime.Instantiate()
and the prefab is properly preloaded and registered, the same guarantees apply.
----------------------
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.
And the OnRealtimeModelReplaced is called before Start and after Awake?
that’s correct
And is it possible to Spawn an object giving it some initialization values from outside? eg. Realtime.Spawn(go); go.GetComponent<X>().SetId(1) and this value of 1 could be taken from OnRealtimeModelReplaced to initialize a model value?
if you set them right after you instantiate they’ll all go out in the same packet
Okay, I'll try that so, thanks 🙂