The recommended way is to write a component, and attach it to the scene element.
The scene and its children will be initialized before this component.
AFRAME.registerComponent('do-something', {
init: function () {
var sceneEl = this.el;
}
});
<a-scene do-something></a-scene>
If for some particular reason you prefer not to write a dedicated component you need to wait for the scene to finish initializing and attaching:
var scene = document.querySelector('a-scene');
if (scene.hasLoaded) {
run();
} else {
scene.addEventListener('loaded', run);
}
function run () {
var entity = scene.querySelector('a-entity');
entity.setAttribute('material', 'color', 'red');
}