1
\$\begingroup\$

I am trying to restart a couple of tweens when I load a new scene. So far, I have this in MainMenuController.cs :

void Start () {
    if (currentScene.name == "MainMenuScene") {
        Debug.Log("In current scene");
        tweenList[0] = mainMenuText.transform.DOShakePosition(5, 10)
                                   .SetAutoKill(false).SetRecyclable(true);
        tweenList[1] = playGameButton.transform.DOShakePosition(5, 10)
                                     .SetAutoKill(false).SetRecyclable(true);
        tweenList[2] = exitGameButton.transform.DOShakePosition(5, 10)
                                     .SetAutoKill(false).SetRecyclable(true);
        DOTween.RestartAll(false);
    }

}

However, this does not start when I reload the scene using

public void ExitToMainMenu() {
        SceneManager.LoadScene("MainMenuScene");
    }

The above is called from another script. Is there some way to restart my tweens when the scene is loaded again?

\$\endgroup\$
2
  • \$\begingroup\$ Is the object that contains MainMenuController preserved between scenes (using DontDestroyOnLoad)? \$\endgroup\$
    – Nikaas
    Commented Jun 25, 2017 at 7:56
  • \$\begingroup\$ Yes, I did that too. But the Tween still doesn't start up. What can I do differently? \$\endgroup\$
    – vkainth
    Commented Jun 25, 2017 at 19:47

1 Answer 1

2
\$\begingroup\$

You don't need to call these:

SetAutoKill(false)
SetRecyclable(true);

Don't mess with DOTween's settings unless you know what you're doing! :)

Also, don't call Start() from another script. Start() is for Unity, not for you :D. Make another method like StartTweens() and do:

public void StartTweens(){

    if (currentScene.name == "MainMenuScene") {
            Debug.Log("In current scene");

            foreach(Tweener tw in tweenList)
                tw.DOKill(); // or tw.Kill();

            tweenList[0] = mainMenuText.transform.DOShakePosition(5, 10);                    
            tweenList[1] = playGameButton.transform.DOShakePosition(5, 10);   
            tweenList[2] = exitGameButton.transform.DOShakePosition(5, 10);

    }
}

Then call StartTweens() in Start().

\$\endgroup\$
4
  • \$\begingroup\$ Thank you! That worked splendidly! Do you know why this works and not my implementation, though? \$\endgroup\$
    – vkainth
    Commented Jun 30, 2017 at 17:14
  • \$\begingroup\$ My guess is that you set SetAutoKill(false) and the tweens were not killed (even if they were finished). Then, without killing them, you assigned new ones and it got messed up. I'd leave the DOTween settings as they are. These settings are for optimization :) Also, you could kill the tweens (with this foreach loop) in the OnDestroy() method from MonoBehavior so when the scene finishes, they'll get destroyed immediately :) You can read about the method here: https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnDestroy.html \$\endgroup\$
    – Jacob
    Commented Jun 30, 2017 at 21:58
  • \$\begingroup\$ Ah! I see what went wrong now. Thanks for the help @Spectre! \$\endgroup\$
    – vkainth
    Commented Jul 1, 2017 at 17:21
  • \$\begingroup\$ No prob! Remember to upvote answers you find helpful :) \$\endgroup\$
    – Jacob
    Commented Jul 1, 2017 at 20:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .