question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Tween/Driver script added even after Stop game in Editor during async method (actually Unity's limitation)

See original GitHub issue

Describe the bug If adding Tween as part of an async method, and stopping the game while it’s still running, the async method will keep running, possibly running tweens and adding dirty Tween Driver components on game objects in the scene, that will stay and be saved in the scene.

Sometimes, you’ll also see error messages about runtime script on that game object, it’s related to the Driver leftover, as removing it removes the error messages.

GameObject (named ‘Team Logo Text (TMP)’) references runtime script in scene file. Fixing!

Unfortunately, this is due to a limitation of Unity that will keep running all async methods after Stop in Editor, as they are not really controlled, and the user may want to use this behaviour for Tool functionality in the Editor.

See: https://forum.unity.com/threads/non-stopping-async-method-after-in-editor-game-is-stopped.558283/

Since there is not much to do to stop this behaviour without adding code on the project itself (see the 2 workarounds in the thread above), I’m only suggestion to, at least, check for if (Application.isPlaying) before adding a component to the game object, the only effect that really bothers me after stopping the game. Obviously, I put more complex stuff in my async method, that will run too and I may have troubles anyway, but at least I’ll be able to avoid the unwanted runtime components sticking to my game objects and being saved in my scene.

To avoid extra code in Builds, you may surround the isPlaying check with #if UNITY_EDITOR

Tested with Graphic Alpha Tween only.

To Reproduce Steps to reproduce the behavior:

  1. Create a new project with package “git+https://github.com/jeffreylanters/unity-tweens”
  2. Create script SplashScreenManager with multiple tweens chained:
using System.Threading.Tasks;
using UnityEngine;

using ElRaccoone.Tweens;

/// Splash Screen Manager
public class SplashScreenManager : MonoBehaviour
{
    [Header("Scene references")]
    
    [Tooltip("Team logo")]
    public GameObject teamLogo;


    private async void Start()
    {
        await PlaySplashScreenSequence();
    }

    private async Task PlaySplashScreenSequence()
    {
        // fade-in
        await teamLogo.TweenGraphicAlpha(1f, 1f).SetFrom(0f).Await();
        await Task.Delay(Mathf.RoundToInt(1000 * 1f));
        // fade-out
        await teamLogo.TweenGraphicAlpha(0f, 1f).Await();
    }
}
  1. Add it to some SplashScreenManager game object
  2. Create Image (it will automatically be added on a Canvas) “TeamLogo” and set a dummy sprite like “Background” on it, just so it’s visible.
  3. Run the game, and try to stop the game at different moments. If you stop if right after fade-in is over (after 1s), it should pop a Driver (meant for the following fade-out) on TeamLogo, that sticks in the scene.

Expected behavior No Driver should be added outside Play

(or they should be removed automatically by some editor script; they probably are on game quit actually, it’s just that another one is re-added afterwards)

Screenshots

image

and if you repeat the operation:

image

Desktop (please complete the following information):

  • OS: Linux Ubuntu
  • Unity: 2021.1.14f1
  • Tween code version: v1.11.1

Additional context I’m testing my splash screen as you may have guessed, after seeing many errors on runtime scripts I spot the issue.

Workaround Use coroutines instead of async methods…

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
hsandtcommented, Jan 9, 2022

OK, I’ll try this.

In the meantime I was using one of the workarounds on the Unity forums, so I didn’t stumble on the issue further.

0reactions
github-actions[bot]commented, Feb 25, 2022

This issue was closed because it has been inactive for 14 days since being marked as stale.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Non-stopping async method after in-editor game is stopped
I'm wondering that the Unity SynchronizationContext should be created per game inside the editor, so when the game is stopped, the game ......
Read more >
Running async code in Unity… in edit mode!
Lately, I've been working quite a lot with Unity's built-in editor ... wanted to have this animation be runnable while still in edit...
Read more >
Async in Unity (better or worse than coroutines?)
While processing an intensive task in the background, so that it doesn't stall your game while it runs, can only really be done...
Read more >
Looking into Unity's async/await
But I would like a cleaner code, I want to use await until the box finished spinning and immediately put a line that...
Read more >
Unity 2023.1 Introduces Awaitable Class
New Awaitable class in Unity 2023.1, methods, properties, usage. Emphasizes understanding async-await & experimenting for best results.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found