当前位置:网站首页>[Solved] Unity Coroutine coroutine is not executed effectively
[Solved] Unity Coroutine coroutine is not executed effectively
2022-08-05 03:06:00 【cat running wild outside the mountain】
Development platform: Unity
Programming platform: Visual Studio 2020 or above
Language used: C#
Problem description
When calling an object that is frequently activated/deactivated, the coroutine (Coroutine) mounted by the object will only be executed when it enters Runtime mode for the first time.Subsequent deactivation and reactivation operations cause the content of the coroutine to not run correctly.
public RectMask2D Mask2D;public Vector4 Forward;public void Start(){StartCoroutine(DOLineMove);}public IEnumerator DOLineMove(){yield return Mask2D.padding -= Forward;StartCoroutinue(DOLineMove());}
Problem Analysis
The premise of running the coroutine is that the mounted GameObject object remains in the Active state.Note: The mounted GameObject object of the coroutine must remain in the Active state during execution or self-iteration).In the Unity life cycle, the initialization phase is Awake , OnEnable , and Start three cycle phases.Roughly as follows:
- The Awake stage is used to capture Component information on the script mount object.
- OnEnable phase is used for event listener registration behavior.
- The Start phase executes the entry procedure.

When Editor first enters Runtime mode, Initialization phase methods will be executed sequentially.So no matter at which stage the StartCoroutine(string methodName) is called, the IEnumerator type is effectively called.The problem is evident at the following levels:
- The internal call
StopCoroutine(string methodName)orStopAllCoroutine()is not standardized.
These two methods disable coroutine objects enabled within the script.One disables only specific objects, the other disables all objects within the script.Irregular use logic is easy to cause this problem. - The script object or the script object itself is automatically disabled and restarted by programs of unknown origin, causing the coroutine to run abnormally.
For example: Awake phase should execute coroutine methods correctly.But in the same Awake cycle stage, another script modified (·SetActive()orenable) state caused.In this case, the coroutine will not function properly.Combined with the execution cycle of the Unity program in Runtime mode, the next time it is enabled, it will be performed directly from theOnEnablestage.Of course, frequent disabling and re-enabling can also be problematic.(This must be a bug written by someone)
Solution: Troubleshoot Who is tampering with (or frequently) the state of the script?
Troubleshooting is often a troublesome thing, which is closely related to the amount of code of the project.It can be resolved quickly, or it can take a few hours, or even a week.Perhaps it was the framework that was not considered problematic at first, and the hidden problems were gradually exposed in the subsequent development.The best way is to follow the design of Loading into the scene on demand, rather than keeping it in the scene, activating, deactivating.If it is too late to save, then the next is the only possible effective method.
1) Use the Quick Find function to check one by one

Visual Studio provides editing for Quick Find .Usually this function is used to query keywords.Of course, it also plays a role in the investigation.Because it is related to disabling and enabling, search for the content associated in SetAtive() / enable in the code, and confirm the troubleshooting one by one.(Time-consuming is inevitable)
2) Use Browse Definitions to quickly locate references

Add anywhere in the program executablethis.gameObject.SetActive(false) or this.enable(true) , right-click the keyword SetActive or enable and select View AllQuote.Visual Studio will give all quoted statements that use this keyword.It is convenient and quick to check.Notice!
- Be sure to open all assemblies for this project when browsing for definitions.rather than individuals.
- In some cases, developers use self-encapsulated
GameObjectUtil.Disable(GameObject obj)and other methods.In this case, it is also necessary to check the object and location information on which the method is called and check one by one.
边栏推荐
猜你喜欢

Use SuperMap iDesktopX data migration tool to migrate ArcGIS data

2022高处安装、维护、拆除考试题模拟考试题库及在线模拟考试

Talking about data security governance and privacy computing

21天学习挑战赛(2)图解设备树的使用

Compressed storage of special matrices

引领数字医学高地,中山医院探索打造未来医院“新范式”

Matlab drawing 3

The 20th day of the special assault version of the sword offer

【七夕节】浪漫七夕,代码传情。将爱意变成绚烂的立体场景,给她(他)一个惊喜!(送代码)

通过模拟Vite一起深入其工作原理
随机推荐
毕设-基于SSM房屋租赁管理系统
(11) Metaclass
QT MV\MVC structure
AI + Small Nucleic Acid Drugs | Eleven Completes $22 Million Seed Round Financing
AI+PROTAC | dx/tx completes $5 million seed round
J9 Digital Currency: What is the creator economy of web3?
2022-08-04: Input: deduplicated array arr, the numbers in it only contain 0~9.limit, a number.Return: The maximum number that can be spelled out with arr if the requirement is smaller than limit.from
Chinese characters to Pinyin
CPDA|How Operators Learn Data Analysis (SQL) from Negative Foundations
人人都在说的数据中台,你需要关注的核心特点是什么?
引领数字医学高地,中山医院探索打造未来医院“新范式”
Matlab画图3
Talking about data security governance and privacy computing
QStyle平台风格
告白数字化转型时代,时速云镌刻价值新起点
Images using redis cache Linux master-slave synchronization server hard drive full of moved to the new directory which points to be modified
高项 02 信息系统项目管理基础
倒计时 2 天|云原生 Meetup 广州站,等你来!
private package
Syntax basics (variables, input and output, expressions and sequential statements)