While passing the level in my game, I am writing this code to delete everything in the level it passed, but why am I getting an error?
//Bölümleri geçme
if(result.other.tags.has("serigec") && this.count ==2 )
{
setTimeout(() => {
var oldHierarchy=this.app.root.findByName("Root");
root.destroy();
this.loadScene(this.sceneId,function(){});
},1500);
}
What error exactly are you receiving?
Check out this tutorial project on changing scenes:
https://playcanvas.com/project/437633/overview/changing-scenes
It should help you with the steps of destroying a scene and loading a new one.
If you would prefer a prebuilt scene manager, I have created one here that you are welcome to fork:
https://playcanvas.com/project/756176/overview/scene-manager
With it, you can fire the ‘changeScene’ event along with the name of the desired scene from anywhere in the app, and it will change the scene for you.
1 Like
https://playcanv.as/b/fekTwWhE/
If you play my game, the objects from the previous scene remain at level 2
Without seeing the way the code and project are set up, it is difficult to provide specific support. With the information available, I can only guess that the objects that remain are not parented by the root element that you’re destroying.
Though taking a quick look, this doesn’t seem right:
root.destroy();
What exactly is root? It is not defined in that function.
this is my code
//Bölümleri geçme
if(result.other.tags.has("serigec") && this.count ==2 )
{
setTimeout(() => {
var oldHierarchy=this.app.root.findByName("Root");
oldHierarchy.destroy();
this.loadScene(this.sceneId,function(){});
},1500);
}
else if(result.other.tags.has("serigecdört") && this.count==4)
{
setTimeout(() => {
var oldHierarchy=this.app.root.findByName("Root");
oldHierarchy.destroy();
this.loadScene(this.sceneId,function(){});
},1500);
}
else if(result.other.tags.has("serigecsekiz") && this.count==8)
{
setTimeout(() => {
var oldHierarchy=this.app.root.findByName("Root");
oldHierarchy.destroy();
this.loadScene(this.sceneId,function(){});
},1500);
}
else if(result.other.tags.has("serigeconalti") && this.count==16)
{
setTimeout(() => {
var oldHierarchy=this.app.root.findByName("Root");
oldHierarchy.destroy();
this.loadScene(this.sceneId,function(){});
},1500);
}
else if(result.other.tags.has("serigecotuziki") && this.count==32)
{
setTimeout(() => {
var oldHierarchy=this.app.root.findByName("Root");
oldHierarchy.destroy();
this.loadScene(this.sceneId,function(){});
},1500);
}
else if(result.other.tags.has("serigecaltmisdört") && this.count==64)
{
setTimeout(() => {
var oldHierarchy=this.app.root.findByName("Root");
oldHierarchy.destroy();
this.loadScene(this.sceneId,function(){});
},1500);
}
else if(result.other.tags.has("serigec128") && this.count==128)
{
setTimeout(() => {
var oldHierarchy=this.app.root.findByName("Root");
oldHierarchy.destroy();
this.loadScene(this.sceneId,function(){});
},1500);
}
else if(result.other.tags.has("serigec256") && this.count==256)
{
setTimeout(() => {
var oldHierarchy=this.app.root.findByName("Root");
oldHierarchy.destroy();
this.loadScene(this.sceneId,function(){});
},1500);
}
else if(result.other.tags.has("serigec512") && this.count==512)
{
setTimeout(() => {
var oldHierarchy=this.app.root.findByName("Root");
oldHierarchy.destroy();
this.loadScene(this.sceneId,function(){});
},1500);
}
else if(result.other.tags.has("serigec1024") && this.count==1024)
{
setTimeout(() => {
var oldHierarchy=this.app.root.findByName("Root");
oldHierarchy.destroy();
this.loadScene(this.sceneId,function(){});
},1500);
}
else if(result.other.tags.has("serigec2048") && this.count==2048)
{
setTimeout(() => {
var oldHierarchy=this.app.root.findByName("Root");
oldHierarchy.destroy();
this.loadScene(this.sceneId,function(){});
},1500);
}
What does this.loadScene
do? I do not see the function listed in your code.
Again, I would recommend looking at the tutorial project to see what steps are taken when a scene is changed. Here is a direct link to the demonstration script:
https://playcanvas.com/editor/code/437633?tabs=5632645
You can also implement the Scene Manager I mentioned before very easily into your project:
https://playcanvas.com/project/756176/overview/scene-manager
1 Like