- Home /
Changing the scene when animation ends
I have two scenes, called scene 1 and scene 2, with .fbx file.
If I want to change the scene from 1 to 2 when animation of scene 1 end, how should I do?
Answer by yummy81 · Feb 13, 2018 at 09:37 AM
Select the gameobject to which your Animator is attached. Then add the script I wrote for you. Next, go to the Animation Window, look at the timeline and right-click at the keyframe for the clip at the end of which you want your scene to load. Then, from the pop-up menu choose "Add Animation Event". Alternatively you can add event, as shown at this picture:
You will see a small bar, so left-click it, look at the inspector window and from the dropdown menu choose the function LoadNewScene(). Don't forget to add your scenes to the Build Settings. Here's the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LoadMyScene : MonoBehaviour
{
public void LoadNewScene()
{
UnityEngine.SceneManagement.SceneManager.LoadScene("scene2");
}
}
Is it ok for me to have 2 or more objects in one scene?
really helpful, did not not know you could do that. FYI, a better way to choose the next scene is to use Scene$$anonymous$$anager.GetActiveScene().buildIndex + 1
to get the next scene and then pass that value into Scene$$anonymous$$anager.LoadScene
Trying to add this in, but it is not working. I added in the code exactly, just copy and paste it into a Scripps folder and saved it. Shouldn't it show up in the animation event function?
Answer by upasnavig90 · Feb 13, 2018 at 09:24 AM
What type of animation is it??, if you are using animations of unity with animators, then you can add event at the end of animation which will load the next scene.
This can Help You, https://docs.unity3d.com/Manual/animeditor-AnimationEvents.html
What do u mean for the type of animation? I used the .fbx files and created 2 objects in both scene 1 and 2.
I want to make a VR app, changing the scene from 1 to 2 without using button when the animation of 2 objects in scene 1 end.
yes, then you can use the animation event triggering as i told you. :)
Answer by TheOrangeNinja2245 · Nov 23, 2018 at 11:46 AM
@yummy81 The script did not work it just comes up as "'default' AnimationEvent 'LoadNewScene()' has no receiver! Are you missing a component?" and i am using Unity 2018.2.16f1. Plz help!
@TheOrangeNinja2245 You must add to the void "public". Example: Not Correct
void SceneAfterAnimation() { Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().buildIndex); } Correct public void SceneAfterAnimation() { Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().buildIndex); }
Your answer
![](https://koobas.hobune.stream/wayback/20220612152527im_/https://answers.unity.com/themes/thub/images/avi.jpg)