- Home /
Why animations are not playing when I go back to previous scene?
Hi Unity Gurus!
I have 2 scenes in my game.
1) MainManu scene
2) Gameplay scene
I am playing animation "Avatar Animation" and "Some leaves" in "MainManu" scene . Avatar animates idle animation and leaves are moving from left to right. Now, When "MainManu" scene executes first time, all animations are working. But, when I go back from gameplay scene to "MainManu" scene animation are not working. Have you any idea about it??
Following code is for leaf movement from left to right.
private Vector3 startRotation;
private Vector3 startPosition;
Transform _t;
public float speed;
public float $$anonymous$$Speed;
public float maxSpeed;
void Awake()
{
_t = transform;
startPosition = _t.position;
startRotation = _t.eulerAngles;
speed = Random.Range($$anonymous$$Speed, maxSpeed);
}
void Update ()
{
Vector3 newRotation = _t.eulerAngles;
Vector3 newPos = _t.position;
newPos.x += Time.deltaTime * speed;
newRotation.z = _t.eulerAngles.z + 5;
_t.eulerAngles = new Vector3(_t.eulerAngles.x, _t.eulerAngles.y, newRotation.z);
_t.position = newPos;
}
void OnBecameInvisible()
{
_t.position = startPosition;
_t.eulerAngles = startRotation;
speed = Random.Range($$anonymous$$Speed,maxSpeed);
}
Answer by Noor-Ali · Apr 14, 2015 at 10:18 AM
If it runs first time but not the second time then you must have done something wrong with global perimeters!
1- If you're moving using thirdparty Tween engine check it's running correctly second time 2- If you're moving manually changing it's transform and you have Time.deltaTime in it. Check your Time.timeScale is "1".
Answer by Diet-Chugg · Apr 01, 2015 at 03:49 PM
from the docs: http://docs.unity3d.com/ScriptReference/MonoBehaviour.Awake.html
Awake is called only once during the lifetime of the script instance
Change Awake() to OnEnable().
from the docs: http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnEnable.html
This function is called when the object becomes enabled and active.
Awake will call only once for the script whereas OnEnable will be called each time you reload the scene as long as the object is not set to DontDestoryOnLoad.
Still not working :( I have changed Awake() to OnEnable() and I tested it. You are saying right. When I back to "mainmanu" scene OnEnable() is called but leaf is not moving.
Your answer
![](https://koobas.hobune.stream/wayback/20220613191908im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Inconsistent Scene loading, Armature Rotation problem. 0 Answers
Animation not playing when returning to a previous loaded scene 2 Answers
Can the animation editor create local rotational data? 3 Answers
Does the Singleton GameObject need to be in all scenes where i need to access it? 2 Answers
Execute Random Animations 1 Answer