Is there a callback for when a scene has loaded?

Hey,
Right now, my React code has a good few instances of using GameObject.FindObjectOfType<> wrapped in setIntervals as often when I initially try find a script/object, the scene has not finished loading (at least this is my assumption), and thus they remain null.
Is there a particular callback I can hook into to guarantee the scene has loaded so I can remove the setIntervals?

Hi there! Iā€™m an AI-powered assistant here to help you. I can assist with questions regarding Needle Engine, three.js, and basic Unity or Blender help. Please reply with your question, including your OS and the versions of Needle Engine, Unity, or Blender you are using for further assistance!

2 replies left - GPT 4O
thread_sxVe62eXdwhN2EJpika5R54N
Reply to this message to continue the conversation

Just use e.g. the onStart hook Needle Engine Scripting | Needle Engine Documentation

You can also use the <needle-engine loadfinished> attribute

Thanks Marcel.

Can you please help me understand a bit more about the onStart, specifically the part in the documentation that says its called once per context. Right now from my App.tsx it seems to be getting called 6 times, and I dont really understand why its being called that many times. I want to just be able to know the scene has loaded, and that Unity components can be found. I assumed this would only need to be run once, not six times.

I have attached a screenshot to show what I mean.

How/where do you invoke the onStart hook now in your app? Can you show your script/more context? It it possible that App.tsx is mounted 6 times or that the function from where you call onStart is called every time your app re-renders? Because if you call onStart multiple times (or add/remove the needle-engine component) your hook will be called once.

Hey Marcus.
Iā€™ve since wrapped this in a use effect. I was calling it outside of any hook in React so it was getting called every re-render.

This topic was automatically closed 13 days after the last reply. New replies are no longer allowed.